0

I have a function and a variable....

var imageURLs = retrieveRemoteImages(vinUrls)

if imageURLs !== undefined) {
   // do something
}

function retrieveRemoteImages(urls) {
    var imageURls = [];
    processSpinnerActivity(START_IMAGES_IMPORT);
    importImagesforSlideShow(REMOTE_IMAGES_URL, urls, function (images) {
        if (images !== undefined) {
            imageURls = images;
            return imageURls;
        }
    })
    return imageURls;
}

This does as intended ....

function importImagesforSlideShow(imagePath, urls, call_back) {
    var functionName = 'importImagesforSlideShow'
    $.ajax({
        type: "POST",
        url: LOCAL_PROCESSING_MODULE,
        data: {data: [imagePath, urls, IMAGE_EXTENSION_QUALIFIER], functionid: functionName},
        dataType:"json",
        success: function (res) {
            processSpinnerActivity(IMPORT_IMAGES_SUCCESS);
            call_back(res);
        },
        error: function (err) {
            processSpinnerActivity(IMPORT_IMAGES_ERROR);
            console.log(err.message);
        }
    });
}

The callback works fine, but I am not able to pass the eventual value to imageURLs and I need to, as the next step cannot occur until it has a value.

Any ideas?

Thanks!

This is not a duplicate question, I have no issues with my AJAX returning the async value*

1 Answers1

1

It's not possible like that, you are executing a piece of async code. So your code keeps running while their isn't any data yet. You have to process your data in a callback, for example like this:

function retrieveRemoteImages(urls, callback) {
    processSpinnerActivity(START_IMAGES_IMPORT);
    importImagesforSlideShow(REMOTE_IMAGES_URL, urls, callback);
}

retrieveRemoteImages(vinUrls, function(imageURLS) {
    //do your stuff
});
Dirk-Jan
  • 1,109
  • 10
  • 21