-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stop execution flow #1035
Comments
We're going to add |
The semantics are not quite the same. I would like to avoid executing the other functions as soon as one returns a value, or not start them at all. |
We don't have a good way to break execution. You can do something like this: function doSomethingAsync(options, callback) {
async.waterfall([
function step1(taskCb) {
// do something
},
function step2(result, taskCb) {
if (someCondition()) {
// exit early
return callback();
}
// continue on
taskCb(null, result);
},
task3
], callback)
} This will prevent function doSomethingAsync(options, callback) {
var stop = {};
async.waterfall([
function step1(taskCb) {
// do something
},
function step2(result, taskCb) {
if (someCondition()) {
// exit early
callback();
// use a fake error
return taskCb(stop);
}
// continue on
taskCb(null, result);
},
task3
], function (err, result) {
if (err === stop) {
// it was the fake error as we break out of the waterfall, ignore
return;
}
callback(err, result);
});
} It's a bit clunky through.... I don't have any good ideas on how to officially support this. Maybe something like |
What about an additional optional callback function passed to all tasks which, if called, immediately stops execution and runs the final callback? |
It would be hard to differentiate that extra callback arg from the regular callback arg. Is the second-to last arg a normal parameter, or the callback? |
Good point. I have no other ideas. |
Related: #552 We need a way to flush the rest of the tasks when you exit early. |
Closing this in favor of #1064 |
What about my proposal |
We're going to use an explicit |
I know this has been asked before, like in #11. I would like to open a discussion about a way to early interrupt any of the collections or flow utilities or provide a new utility for that purpose.
I often find myself needing to execute several asynchronous operations (usually in series, but the same would apply to a parallel scenario) until one of them returns a result.
I would normally implement this using series or waterfall, except that I would like to exit early and avoid running the following operations in the chain.
Also, I would like to avoid having to signal an error to do that, which I would then have to analyze specifically in the final callback to check whether it is a legitimate error or the special chain-breaking error.
The text was updated successfully, but these errors were encountered: