following is a short example for using a promise with Q.
this is test1.js:
function testDefer() {
var deferred = Q.defer();
fs.readFile(\
All errors in then handlers are caught and used to reject the resulting promise. What you want is the done method:
Much like
then, but with different behavior around unhandled rejection. If there is an unhandled rejection, either becausepromiseis rejected and noonRejectedcallback was provided, or becauseonFulfilledoronRejectedthrew an error or returned a rejected promise, the resulting rejection reason is thrown as an exception in a future turn of the event loop.This method should be used to terminate chains of promises that will not be passed elsewhere. Since exceptions thrown in
thencallbacks are consumed and transformed into rejections, exceptions at the end of the chain are easy to accidentally, silently ignore. By arranging for the exception to be thrown in a future turn of the event loop, so that it won't be caught, it causes anonerrorevent on the browserwindow, or anuncaughtExceptionevent on Node.js'sprocessobject.The Golden Rule of
donevs.thenusage is: eitherreturnyour promise to someone else, or if the chain ends with you, calldoneto terminate it.
Q.ninvoke(fs, "readfile", "foo.txt", "utf-8").done(function(data){
console.log('all good');
}, function(err) {
throw new Error('I want to throw this exception');
}); // or omit the error handler, and 'err' will be thrown