0

I'm working on a project with the amazing async.js library. Trying to understand the use of promises, but I can't.

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:

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:

(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.

I have seen few answers regarding the same, but I still can't solve it.

robe007
  • 3,523
  • 4
  • 33
  • 59

1 Answers1

1

I know it's not a good idea to use Promises with 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(); call in the then callback, the promise will reject. And not only that, the catch on that rejected promise will also execute, now calling 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. 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:

connect().then(function (result) {
    console.log(result);
    next(null, e);
}, function (e) {
    console.log("An error here");
    next(e);
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Sorry, but...what means 'the former'? – robe007 Sep 05 '17 at 00:18
  • 1
    @robe007 It refers to `.then(…, …)`, while `.then(…).catch(…)` would be "the latter" – Bergi Sep 05 '17 at 01:03
  • Thanks my friend for your contribution. Can you please, explain a little bit more about the `difference between .then(…, …) and .then(…).catch(…)` here in my answer. – robe007 Sep 06 '17 at 00:39
  • @robe007 No. There's a link, and I don't intend to repeat everything I've written there. What in particular is unclear? – Bergi Sep 06 '17 at 00:43