jQuery ajax success not changing local variable.
===

I'm trying to use ajax in order to check if an image is there before trying to load it. I have it mostly working, but I'm setting a variable inside of the error: and success: tags in ajax, and it isn't changing my img variable. Is there any way to effect the rest of the function through these tags?

    function getImage(imageNum)
    {
    var img;
    $.ajax({
        url:imageLocation + imageNum + '.png',
        type:'HEAD',
        error: function()
        {
            img = 0;
        },
        success: function()
        {
            var img = $('<img />', {
    			src: imageLocation + imageNum + '.png',
    			class: 'image',
    			id:'image' + imageNum});
          	return img;
        }
    });
    console.log(img);
    return img;
    }