Lugia101101
Lugia101101

Reputation: 671

get return value of jQuery get().done() function

I have the code:

function images(){
    $.get("page").done(function(data) {
        manipulation to get required data
    });
}

and I have tried to get a variable from inside the get function, using global variables, return values, and functions outside of the function. Does anyone know how I would be able to get a variable from inside the get function and return it as a value if I called images()?

Upvotes: 1

Views: 11499

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388416

You can't do it since $.get() executes asynchronously. The solution is to use a callback in images to process the value returned by $.get()

function images(callback){
    $.get("page").done(function(data) {
        manipulation to get required data
        var d = ? //manipulated data that was supposed to be returned
        callback(d);
    });
}

then

//calls images
images(function(data){
    //this method will get called when the $.get() is completed, and data will be the value passed to callback(?) in the done function
})

More: read this

Upvotes: 11

Related Questions