Skip to content

Instantly share code, notes, and snippets.

@staltz
Created May 3, 2014 05:59
Show Gist options
  • Save staltz/c926a7f47e63cd3f0c18 to your computer and use it in GitHub Desktop.
Save staltz/c926a7f47e63cd3f0c18 to your computer and use it in GitHub Desktop.
Q promises single error catcher
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() {
return delaydo(2);
})
.then(function() {
return delaydo(3);
})
.then(function() {
return delaydo(4);
})
.catch(function(err) {
console.log("Caught err: "+err);
});
resolve: 1
reject: 2
Caught err: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment