var fs = require('fs');
var async = require('async');
async.waterfall([
(cb) => fs.readFile('foo.txt', cb),
(data, cb) => my_function(data, cb),
(result, cb) => myDb.lookup(result, cb),
], (err, finalResult) {
if (err) {
console.error(err);
} else {
console.log(finalResult);
}
});
There are all sorts of helpful async primitives in there. I don't write Javascript without it!Here is a link to the documentation: https://github.com/caolan/async
If you're trying to convince me that "callbacks become painful in situation X" you really don't need to. I know that. What I'm saying is that 9 times out of 10, the answer to the question "do we really want to be in situation X in the first place?" is "no". But instead of getting out of a bad situation by refactoring, people just write crazier and crazier control structures (i.e. promises) to make those bad situations workable.
https://nodejs.org/api/domain.html
But it was deprecated: https://github.com/nodejs/node/issues/66
promises are a very nice way of drying out error handling and cleaning up callbacks for now.