I have a jQuery function/plugin that returns a value but there is a function that is called before returning that needs to be finished before returning or else the value returned is incorrect.
If I put the return line inside function1().done, it doesn't seem to return anything at all. If I put it outside, it returns before function1 is finished.
Below is not the real code but I am using this example to convey my problem. Hope it makes sense.
$(function(){
function1().done(function(){
console.log('function1 is done!');
return "this value needs to be returned only after function1 above is finished";
});
});
function function1(){
var dfrd1 = $.Deferred();
var dfrd2= $.Deferred();
setTimeout(function(){
// doing async stuff
console.log('task 1 in function1 is done!');
dfrd1.resolve();
}, 1000);
setTimeout(function(){
// doing more async stuff
console.log('task 2 in function1 is done!');
dfrd2.resolve();
}, 750);
return $.when(dfrd1, dfrd2).done(function(){
console.log('both tasks in function1 are done');
// Both asyncs tasks are done
}).promise();
}