Skip to content

Instantly share code, notes, and snippets.

@staltz
Created May 3, 2014 06:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save staltz/ea10ce04cef14066816c to your computer and use it in GitHub Desktop.
Save staltz/ea10ce04cef14066816c to your computer and use it in GitHub Desktop.
Q promises pyramid of doom
var Q = require('q');
function delaydo(x) {
var deferred = Q.defer();
setTimeout(function() {
if (x === 2) {
console.log("reject: "+x);
deferred.reject(x);
}
else {
console.log("resolve: "+x);
deferred.resolve(x);
}
}, 1000);
return deferred.promise;
}
delaydo(1)
.then(function() {
delaydo(2)
.then(function() {
delaydo(3)
.then(function() {
delaydo(4);
})
.catch(function(errC) {
console.log("Error handler C: "+errC);
});
})
.catch(function(errB) {
console.log("Error handler B: "+errB);
});
})
.catch(function(errA) {
console.log("Error handler A: "+errA);
});
resolve: 1
reject: 2
Error handler B: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment