javascript - Chaining more than two tasks using deferred and can this be use for loops? -
i getting used $.deferred
now, , happens needed chain 3 tasks using $.deferred - then
. created function:
function processa(param1, param2) { log = $.post('http://domain.com/process', { id: param1, another: param2 } ), set = log.then(function(html){ if (somecondition) { console.log('successful'); // want ajax call here // , chain another, ajax again } }) }
how do that, stated in comments of code. right? not tested, thought while typing this.
set = log.then(function(html){ if (somecondition) { console.log('successful'); $.post(....), def = set.then(function(data){ // want thing here. }) } })
and thing, possible use function in loop? e.g.
data = [{param1:"...", param2:"..."}, {..., ...}] $.each(data, function(k,v){ processa(v.param1, v.param2); })
here better explained chaining promises:
function authenticate() { return getusername() .then(function (username) { return getuser(username); }) // chained because not need user name in next event .then(function (user) { return getpassword() // nested because need both user , password next .then(function (password) { if (user.passwordhash !== hash(password)) { throw new error("can't authenticate"); } }); }); }
Comments
Post a Comment