-
-
Save kurdin/6d0a2b7a8ca3b724e648 to your computer and use it in GitHub Desktop.
combine multiple chains with jQuery.Deferred
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.when( | |
sushi_roll(), | |
dispatch() | |
).done(function() { | |
console.log('accepted:', arguments[0], arguments[1]); | |
}); | |
var n = 0; | |
function dispatch() { | |
return $.Deferred(function(dispached) { | |
$.when.apply(this, $.map(['genmai-cha','soba','tenpura'], function(v) { | |
return $.Deferred(function(dfd) { | |
setTimeout(function() { | |
console.log("resolve "+v); | |
dfd.resolve(v); | |
}, Math.random() * 1500); | |
}) | |
.done(function(msg) { | |
console.log('done:'+msg); | |
}); | |
})) | |
.done(function() { | |
console.log('done all'); | |
dispached.resolve('dispache done'); | |
}); | |
}); | |
} | |
function sushi_roll() { | |
return $.Deferred(function(d) { | |
$.when( | |
proc_a(), | |
proc_b() | |
) | |
.done(function(deco,rice) { | |
console.log('accepted:',deco,rice); | |
d.resolve('roll'); | |
}); | |
}); | |
} | |
function proc_a() { | |
return $.Deferred(function(d) { | |
setTimeout(function() { | |
d.resolve('set tsuna'); | |
}, Math.random() * 1000); | |
}); | |
} | |
function proc_b() { | |
return $.Deferred(function(d) { | |
setTimeout(function() { | |
d.resolve('set rice'); | |
}, Math.random() * 2000); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment