NodeJS async.waterfall — Untold facts

Conventional waterfall model

   1. Get employee details from DB
2. Calculate his salary based on his package and leave details
3. Do a bank transfer
4. Notify employee by email with pay slip
getEmployeedetails(args, function() 
{ calcSalary(args, function()
{ backTransfer(args, function()
{ notifyEmployee…
})
})
})
})

Manageable with async.waterfall

var async =  require('async');
/*-----------start of async call ------------*/
async.waterfall([
function (callback){
callback(null, 'Get details is step 1');
},
function (startparam, done) {
console.log('Sal param - '+ startparam);
done(null, 'after salary from step 2'); // <- set value to passed to step 3
},
function (step1Result, done) {
console.log(step1Result);
done(null, 'after transfer from step 3'); // <- set value to passed to step 4
},
function (step2Result, done) {
console.log(step2Result);
done(null, 'mail sent - last step');
}
],
function (err) { /*-----------final callback from async call ------------*/
if (err) {
console.log ('error');
} else {
console.log('No error happened in any steps, operation done!');
}
});
/*-----------end of async call ------------*/
Log from above code

Named functions

var async =  require('async');
/*-----------start of async call ------------*/
async.waterfall([
getEmployeedetails,
calcSalary,
backTransfer,
notifyEmployee

],
function (err) { /*----final callback from async call --------*/
if (err) {
console.log ('error');
} else {
console.log('No error happened in any steps, operation done!');
}
});
/*-----------end of async call ------------*/
/*-----------step functions inside call ------------*/
function getEmployeedetails(callback){
callback(null, 'Get details is step 1');
}
function calcSalary(startparam, done) {
console.log('Sal param - '+ startparam);
done(null, 'after salary from step 2'); // <- set value to passed to step 2
}
function backTransfer(step1Result, done) {
console.log(step1Result);
done(null, 'after transfer from step 3'); // <- set value to passed to step 3
}
function notifyEmployee (step2Result, done) {
console.log(step2Result);
done(null, 'mail sent - last step');
}

In parameter to first function in stack

var async =  require('async');
/*-----------start of async call ------------*/
var empId = 1014;
async.waterfall([
function setParam (allset){
allset(null, empId);
},
getEmployeedetails,
calcSalary,
backTransfer,
notifyEmployee
],
function (err) { /*----final callback from async call --------*/
if (err) {
console.log ('error');
} else {
console.log('No error happened in any steps, operation done!');
}
});
/*-----------end of async call ------------*/
/*-----------step functions inside call ------------*/
function getEmployeedetails(dummysetup, callback){
console.log ('From setParam - ' + dummysetup);
callback(null, 'Get details is step 1');
}
function calcSalary(startparam, done) {
console.log('sal param - '+ startparam);
done(null, 'after salary from step 2'); // <- set value to passed to step 2
}
function backTransfer(step2Result, done) {
console.log(step2Result);
done(null, 'after transfer from step 3'); // <- set value to passed to step 3
}
function notifyEmployee (step3Result, done) {
console.log(step3Result);
done(null, 'mail sent - last step');
}
Log from above code

Meaningful callback names

var async =  require('async');
/*-----------start of async call ------------*/
var empId = 1014;
async.waterfall([
function setParam (allset){
allset(null, empId);
},
getEmployeedetails,
calcSalary,
backTransfer,
notifyEmployee
],
function (err) { /*--final callback from async call ------*/
if (err) {
console.log ('error');
} else {
console.log('No error happened in any steps, operation done!');
}
});
/*-----------end of async call ------------*/
/*-----------step functions inside call ------------*/
function getEmployeedetails(dummysetup, getDone){
console.log ('From setParam - ' + dummysetup);
getDone(null, 'Get details is step 1');
}
function calcSalary(startparam, saldone) {
console.log('sal param - '+ startparam);
saldone(null, 'after salary from step 2');
}
function backTransfer(step2Result, xfrdone) {
console.log(step2Result);
xfrdone(null, 'after transfer from step 3');
}
function notifyEmployee (step3Result, notifydone) {
console.log(step3Result);
notifydone(null, 'mail sent - last step');
}
  1. To work over say an array of Employee
  2. To error out and stop processing for a particular employee and continue with the rest.
  3. Report a list of errored and successful process out of an array

--

--

--

An artist , writer, web developer, who cares to share and clap at deep insights, humor, lateral thoughts and passionate pursuits.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Are you sure you know how an event propagates in JavaScript?

Facts you should know before starting js

The Complete Guide to Angular Testing and a Weird Trick for Faster Unit Tests

Midterm Update

Basic Javascript

When You Should Ignore the JavaScript Frontend Framework Hysteria

Introducing Vue Datatable

Exploring Pipes in Angular

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Patience Phillips

Patience Phillips

An artist , writer, web developer, who cares to share and clap at deep insights, humor, lateral thoughts and passionate pursuits.

More from Medium

Apollo Server Errors for Clients with TypeScript

Upload Images to Cloudinary using NodeJS

Uninstall, deep-clean and re-Install Node.js — Ubuntu best practice

Remove all IIFEs