简体   繁体   English

async.js瀑布内未处理的承诺拒绝警告

[英]Unhandled Promise Rejection Warning inside async.js waterfall

I'm working on a project with the amazing async.js library. 我正在使用惊人的async.js库进行项目。 Trying to understand the use of promises, but I can't. 试图了解Promise的用法,但我做不到。

I implemented the following code: 我实现了以下代码:

function connect(){
    return new Promise(function (resolve, reject) {
        bar.getConnection( server, function( err, conn ){
            if( err ) {
                reject("An error. " + err);
            }
            else{
                resolve("Ok. Connected to: " + conn.serverAddress);
            }
        });
    });
}

And then in the async waterfall : 然后在async waterfall

exports.getRequest = function( callbk ){
    (function(callback) {
        async.waterfall([
            function (next) {
                connect().then(function (result) {
                    console.log(result);
                    next();
                }).catch(function (e) {
                    // If something gets an error on the next function, this catch fires
                    // And the 'next(e)' does not execute
                    console.log("An error here");
                    next(e);
                });
            },
            function (next) {
                // do something .... and get a result
                // but if something gets an error here, fires the 'catch' on 'connect'
                next(null, result);

            },
            function (err, result) {
                if(err) {
                    callback(true, "" + err);
                }
                else {
                    callback(false, result);
                }
            }
        ]);
    })(function(error, result) {
        callbk(error, result);
    });
}

But, if something gets wrong in the second function inside the 'waterfall' the catch of the first function rise up, and it comes with: 但是,如果在“瀑布”内部的第二个功能出了问题,则第一个功能的catch上升,并且随之而来:

(node:8984) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Callback was already called.
(node:8984) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I know it's not a good idea to use Promises with async.js, but I want to understand why. 我知道将Promises与async.js一起使用不是一个好主意,但我想了解原因。

I have seen few answers regarding the same, but I still can't solve it. 对于相同的问题,我几乎看不到答案,但仍然无法解决。

I know it's not a good idea to use Promises with async.js 我知道将Promises与async.js一起使用不是一个好主意

Good! 好!

but I want to understand why. 但我想知道为什么。

If anything in one of your callbacks (including the one passed to getRequest ) does throw an exception from that next(); 如果您的回调(包括一个传递到一个什么 getRequest )不从抛出异常next(); call in the then callback, the promise will reject. then回调中调用时,promise将拒绝。 And not only that, the catch on that rejected promise will also execute, now calling next(e); 而且不仅如此,对被拒绝的承诺的catch也将执行,现在调用next(e); - which will make async.js complain about the next callback getting called twice, ignoring e and rejecting the second promise with the new exception. -这会使async.js抱怨next回调两次被调用,忽略e并拒绝带有新异常的第二个承诺。 This rejection isn't handled anywhere and does get logged to your console. 拒绝在任何地方都不会处理,并且会记录到您的控制台中。

Have a look at the difference between .then(…, …) and .then(…).catch(…) - if you use the former, then the original exception will reject the promise and get logged as unhandled, with no callback being called twice: 看看.then(…, …).then(…).catch(…)之间区别 -如果使用前者,则原始异常将拒绝promise并记录为未处理,没有回调叫了两次:

connect().then(function (result) {
    console.log(result);
    next(null, e);
}, function (e) {
    console.log("An error here");
    next(e);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM