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.