Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created July 1, 2020 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velotiotech/816381cf4dd60aee0156463e87f192e6 to your computer and use it in GitHub Desktop.
Save velotiotech/816381cf4dd60aee0156463e87f192e6 to your computer and use it in GitHub Desktop.
NodeJS Async Waterwall
function foo(arg, callback) {
if (arg < 0) {
callback('error');
return;
}
callback(null, arg+1);
}
function bar(arg, callback) {
if (arg < 0) {
callback('error');
return;
}
callback(null, arg+2);
}
function baz(arg, callback) {
if (arg < 0) {
callback('error');
return;
}
callback(null, arg+3);
}
async.waterfall([
(cb) => {
foo(0, cb);
},
(arg, cb) => {
bar(arg, cb);
},
(arg, cb) => {
baz(arg, cb);
}
], (err, result) => {
if (err) {
console.log(err);
} else {
console.log(result); //6
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment