var Q = require('q');
var step1 = function() {
var deferred = Q.defer();
setTimeout(function() {
console.log('step1');
deferred.resolve();
}, 800);
return deferred.promise;
}
var step2 = function() {
var deferred = Q.defer();
setTimeout(function() {
console.log('step2');
deferred.resolve();
}, 600);
return deferred.promise;
}
var step3 = function() {
var deferred = Q.defer();
setTimeout(function() {
console.log('step3');
deferred.resolve();
}, 400);
return deferred.promise;
}
var step4 = function() {
var deferred = Q.defer();
setTimeout(function() {
console.log('step4');
deferred.resolve();
}, 200);
return deferred.promise;
}
// for the following calls
step1();
step2();
step3();
step4();
// output
// step4
// step3
// step2
// step1
step1().then(step2).then(step3).then(step4).done();
// output
// step1
// step2
// step3
// step4
Friday, May 24, 2013
Understanding Q-Deferred in NodeJS through example
Subscribe to:
Posts (Atom)