I have four functions which are running parallely. If any one function fails in between, how can I stop the execution of other functions. Any help on this will be really helpful.
3 Answers
Except if it's a setTimeout or setInterval, I think you can't. You can, anyway, set checkpoints in the logic of the functions. It's not clean a all, but it can work. For example, checking the other function in the callback:
var control = true;
async1(function(e,r){
if(e) {
control = false;
return callback1(e,r);
};
if(control) callback1(e,r);
});
async2(function(e,r){
if(e) {
control = false;
return callback2(e,r);
};
if(control) callback2(e,r);
});
Althougt to do this, I would go with throrin19 and say that Async it's a nice lib to do this.
But maybe you would like to check co. It could handle your problem better than Async.

- 3,316
- 1
- 25
- 30
-
fun1() { // some I/O operation // checking status in DB if ( status === "failed") throw new Error("Failed"); } fun2() { //Waiting for http response } Both fun1() and fun2() are running parallely. If fun1() fails, program should terminate. – user87267867 Oct 15 '13 at 08:53
-
If you want to finish the program, you can do a plain `process.exit()`. If you want to just cancel the http request, dont waiting for the response, you could close the stream with `.destroy()`. This should work if I understood you correctly. – durum Oct 15 '13 at 10:28
-
You can see how to abort a request more detailed here: http://stackoverflow.com/questions/16194017/stop-downloading-the-data-in-nodejs-request – durum Oct 15 '13 at 10:35
-
Thanks for your quick response. There can be n number of functions. As in my code u can see fun1() failed, so fun2() and other parallel n functions should be stopped from processing. – user87267867 Oct 15 '13 at 11:39
-
@vkurchatkin Apart from process.exit(), is there any other way to break the process? I want exactly wat process.exit() does but process should not exit. – user87267867 Oct 15 '13 at 12:37
-
A really powerful solution (but somewhat complicated if you dont know well node.js) is make a cluster and set all the n functions in one fork and then. If one of them fails, kill the fork. I'm afraid that can be somewhat complicated to some people to do it, but it can be a solution. You can see more of cluster here: http://nodejs.org/api/cluster.html – durum Oct 15 '13 at 13:11
-
Can you explain with some example? If I am not wrong cluster is used to balance the load with the help of master and workers – user87267867 Oct 16 '13 at 03:47
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39333/discussion-between-user87267867-and-bduran) – user87267867 Oct 16 '13 at 09:34
You can't stop execution of a function. Functions are executed and return synchronously, so there is technically nothing to stop. But there can be asynchronous tasks, which use underlying libuv capabilites somehow (that is, you can't do anything asynchronous without calling some asynchronous node api or some native module). Functions are only interfaces for such tasks and they don't support canceling tasks, only starting.
So, you can't really cancel async operations, but what you can do is ignore the results of other operations if one fails. Here is how it can be implemented:
var tasks = [], //array of tasks, functions accepting callback(err)
pending = tasks.length, //amount of pending tasks
failed = false;
function done(err) { //callback for each task
if (failed) return;
if (err) {
failed = true;
callback(err); //callback for all tasks
}
if (!--pending) callback(); //all tasks completed
}
tasks.forEach(function(task) {
task(done);
});

- 13,364
- 2
- 47
- 55
-
but I guess you can achieve something like this using async. Though internally it does something like this – vkurchatkin Oct 15 '13 at 10:38
-
Right. Althougth your aproach it's different of mine, because you only get the first error. What is better? It really depends on what the coder wants to do. The most abstract async function to do this it's parallel: https://github.com/caolan/async#paralleltasks-callback – durum Oct 15 '13 at 10:55
-
Yes, it depends, but since we are trying to emulate canceling mine approach seems to be closer (as if we really cancel other operations they would reach neither success nor failure). By the way, it looks that async.parallel has the same exact behaviour – vkurchatkin Oct 15 '13 at 11:28
I think you should use the Async library. Async contains parallel function that executes functions in parallel (of course). And each function takes a callback (error, result). If an error occurs, Async will cut all other processes.

- 17,796
- 4
- 32
- 52
-
1The only problem, Async doesn't stop the execution of the other functions, it simly ignores it. – Leonid Beschastny Oct 15 '13 at 07:00