I have came cross several answers and comments suggesting to avoid nested callbacks in NodeJS. I believe there is a strong point of view behind that but I can not get it properly!
Let's assume that I have the following code
module1.func1(
function(){
//some code
module2.func2(
function(){
//some code
module3.func3(etc....)
}
);
}
);
Now let's assume that the code I would write in the callback may cause an exception then try and catch must be added to the code
module1.func1(
function(){
try{
//some code
module2.func2(
function(){
try {
//some code
module3.func3(etc....)
} catch (ex){//handle exception}
}
);
} catch(e){//handle exception}
}
);
Now, I'm not only having nested callbacks but also with exceptions-handled callbacks which add more overhead on memory!
Some would suggest using step, async, wait-for but I don't believe they are good solutions from performance perspective as they only provide simpler syntax and that's it. Correct me if I'm wrong.
Is there any way to avoid such problem? to improve performance of callbacks-nested-code?
Promises automatically propagate both asynchronous and synchronous errors
What does that mean?
It means code that callback code that wishes to propagate errors properly:
Can be replaced with:
Try Node.js Domain core module: http://nodejs.org/api/domain.html