最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - throw Error after promise is rejected - Q - Stack Overflow

programmeradmin0浏览0评论

following is a short example for using a promise with Q.

this is test1.js:

function testDefer() {
    var deferred = Q.defer();
    fs.readFile("foo.txt", "utf-8", function (error, text) {
        if (error) {
            deferred.reject(new Error(error));
        } else {
            deferred.resolve(text);
        }
    });
    return deferred.promise;
}

and this is test2.js

(function(){
    'use strict';
    var test1 = require('./test1');
    test1.testDefer().then(
        function(data){
            console.log('all good');
        },
        function(err) {
            //upon error i might want to throw an exception, however, it is not thrown / ignored.
            throw new Error('I want to throw this exception');
        }
    );
})();

i want to throw an exception in test2 in case the promise is rejected (or in some cases when it's resolved). anyway, the exception is ignored and the program finishes without throwing the exception.

my question is, how to throw errors from the success/failure functions?

thank you

following is a short example for using a promise with Q.

this is test1.js:

function testDefer() {
    var deferred = Q.defer();
    fs.readFile("foo.txt", "utf-8", function (error, text) {
        if (error) {
            deferred.reject(new Error(error));
        } else {
            deferred.resolve(text);
        }
    });
    return deferred.promise;
}

and this is test2.js

(function(){
    'use strict';
    var test1 = require('./test1');
    test1.testDefer().then(
        function(data){
            console.log('all good');
        },
        function(err) {
            //upon error i might want to throw an exception, however, it is not thrown / ignored.
            throw new Error('I want to throw this exception');
        }
    );
})();

i want to throw an exception in test2 in case the promise is rejected (or in some cases when it's resolved). anyway, the exception is ignored and the program finishes without throwing the exception.

my question is, how to throw errors from the success/failure functions?

thank you

Share Improve this question asked Mar 5, 2014 at 12:49 Asher SabanAsher Saban 4,80313 gold badges49 silver badges61 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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 because promise is rejected and no onRejected callback was provided, or because onFulfilled or onRejected threw 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 then callbacks 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 an onerror event on the browser window, or an uncaughtException event on Node.js's process object.

The Golden Rule of done vs. then usage is: either return your promise to someone else, or if the chain ends with you, call done to 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
发布评论

评论列表(0)

  1. 暂无评论