i have function :
var = function(){ var = this; var datas = ["data1", "data2",.., "datan"]; var dfd = new $.deferred(); $.each(datas, function(i,el){ firstasynccall(el); //it asynchrounus stuff }); secondasynccall(); dfd.resolve(); return dfd.promise(); } then
var b = function() { a.done( function() { //other async stuff }); } my problem callback inside .done() not executed after every async call inside a().
how can fix that? how can execute callback after a() executed ?
notice firstasynccall secondasynccall , callback inside .done() asynchronous stuff
it depends on wht trying achieve, let's assume :
secondasynccall()executed after allfirstasynccall()calls have completed- the "other async stuff" should happen after
secondasynccall()has completed.
firstly, make sure firstasynccall() , secondasynccall() each returns promise.
var = function() { var datas = ["data1", "data2",.., "datan"]; var firstpromises = $.map(datas, function(el, i) { return firstasynccall(el); //it asynchrounus stuff }); //at point, firstpromises array of promises. return $.when.apply(null, firstpromises).then(secondasynccall); }; you can write :
var b = function() { return a().then(function() { //other async stuff }); }; by returning promise generated a().then(...), can chain further actions eg b().then(...), b().done(...), b().fail() or b().always().
Comments
Post a Comment