如果要保持简单,可以使用基于计数器的回调系统。这是允许
when(A, B).then(C)语法的系统草案。(
when/
then实际上只是糖,但整个系统还是有争议的。)
var when = function() { var args = arguments; // the functions to execute first return { then: function(done) { var counter = 0; for(var i = 0; i < args.length; i++) { // call each function with a function to call on done args[i](function() { counter++; if(counter === args.length) { // all functions have notified they're done done(); } }); } } };};用法:
when( function(done) { // do things done(); }, function(done) { // do things setTimeout(done, 1000); }, ...).then(function() { // all are done});


