在承诺被拒绝后抛出错误-Q [英] throw Error after promise is rejected - Q

查看:31
本文介绍了在承诺被拒绝后抛出错误-Q的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是在Q中使用promise的简短示例.

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

这是test1.js:

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;
}

这是test2.js

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');
        }
    );
})();

我想在test2中抛出一个异常,以防承诺被拒绝(或在某些情况下被解决).无论如何,该异常将被忽略,并且程序将在不引发该异常的情况下完成.

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?

谢谢

推荐答案

then 处理程序中的所有错误均被捕获并用于拒绝所产生的承诺.您想要的是 完成方法:

All errors in then handlers are caught and used to reject the resulting promise. What you want is the done method:

非常类似于 then ,但未处理的行为不同拒绝.如果存在未处理的拒绝,则可能是因为 promise 被拒绝并且没有提供 onRejected 回调,或者因为 onFulfilled onRejected 抛出错误或返回了被拒绝的承诺,导致的被拒绝原因被抛出为将来发生事件循环时会出现异常.

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.

此方法应用于终止应许的承诺链不能传递到其他地方.由于在 then 回调中引发了异常被消耗并转化为拒绝,末尾的异常连锁店很容易意外,默默无视.通过安排在以后的事件循环中抛出的异常,因此它不会被捕获,它会在浏览器上导致 onerror 事件 window 或Node.js的 process 上的 uncaughtException 事件对象.

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.

完成 vs. 然后的黄金法则是: return your向其他人保证,或者如果链条以您结尾,请致电 done 终止它.

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

这篇关于在承诺被拒绝后抛出错误-Q的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆