Everything in nodejs is non-blocking which is nice, but how would I go about making function alls that have to be one after the other without having a huge nested list of callbacks?
Everything in nodejs is non-blocking which is nice, but how would I go about making function alls that have to be one after the other without having a huge nested list of callbacks?
Share Improve this question asked Mar 20, 2011 at 2:39 AdamBAdamB 3,1214 gold badges35 silver badges45 bronze badges 1- 2 Node.js is meant for non-blocking code. You're going against the flow and defeating the very purpose it exists by trying to make it blocking. – Ruan Mendes Commented Feb 8, 2012 at 22:13
5 Answers
Reset to default 13You don't have to nest your callbacks.
There are many patterns in writing asynchronous code.
For instance, this matrioska-nested-style...
database.find('foo', function (err, data) {
database.update('foo', 'bar', function (err, data) {
database.delete('bar', function (err, data) {
console.log(data);
});
});
});
... can be rewritten in a cleaner (but more verbose) way:
var onDelete = function (err, data) {
console.log(data);
},
onUpdate = function (err, data) {
database.delete('bar', onDelete);
},
onFind = function (err, data) {
database.update('foo', 'bar', onUpdate);
};
database.find('foo', onFind);
Another option is using a module to abstract serial and parallel execution of callbacks.
Use Step.
It's "a simple control-flow library for node.JS that makes parallel execution, serial execution, and error handling painless".
What you actually want to do is find out why your operations are blocking and recode them so they are non-blocking. Remove the dependencies on each other. You need to change the way you're thinking about non-blocking IO.
Using a library to allow you to run this type code in a synchronous blocking manner is just a poor crutch.
You will be significantly better off learning how to write non blocking code in node.js because that's what it is designed to do.
The async module is a particularly good solution - using it results in much cleaner asynchronous code, with much shallower nesting. You can get it via:
npm install async --save
Especially take a look at:
- async.series: this lets you set up a list of functions to run one after another - where each in the list runs only after the one before it has completed. However, other code (outside the defined series) can run without blocking.
- async.series: this is similar to async.series, except each function on the list passes its result to the next in the list, with the final result passed to a callback defined at the end.
However, all of async's control flow-specific methods are very helpful for avoiding huge nested callback lists.
Also, if it helps, here's a jsFiddle I put together when learning the library, containing a set of examples, including one for async.waterfall & another for async.series (open the console to see what it's doing).
use ES6 async and await method's for writing blocking code
====================================================================
Ex:
async function fName(){
lat firstResult=await task1;
let secondResult= await task2
}
function task1(){
//write your logic and after finishing your task return result as a PROMISE
return new Promise((reslove,reject)=>{
// write logic hear after reslove(success) or reject(error)
})
}
function task2(){
//write your logic and after finishing your task return result as a PROMISE
return new Promise((reslove,reject)=>{
// write logic hear after reslove(success) or reject(error)
})
};