-
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
async.race = execute in parallel, but only use the first result #568
Comments
|
The way I understand In my case I'd like to run several (different) async functions, and return the result of the first one to finish. // we have potential services that can return the same data
// call both in parallel and return whichever comes back first
async.first([
getDataFromServiceX,
getDataFromServiceY
], done); or // call done() when the long operation finishes
// or after 1 sec, whichever happens first
async.first([
function(next) { longRunningOperation(next); }
function(next) { setTimeout(next, 1000); }
], done); |
Yeah, I was wrong.. Haha |
No worries :) |
I would like this feature too =) Now I hacked async.parralel so that it returns data in err and err in data, but it is really ugly and confusing. |
+1 do want |
an alternate mode would be to ignore errors, and keep waiting for the first non-failure response, until all have been attempted |
+1 |
@rprieto you might need to create some logic to check the result of the request. for example, if the request returns an error, we might not want the first one, or if the first request doesn't have the data you want, you might not want the first result either. So there might need to be some conditional check in there somewhere. So for generic usage, we want not just the first result but the first result that meets some condition. So your version of async.first might want to take one more argument in the form of a function that contains some conditional logic that returns a boolean. |
something that looks like: async.first = function(ops, func, done) { // developer defined func to check condition
var length = ops.length;
var responses = 0;
var success = false;
for (var i = 0; i < length; i++) {
ops[i](function(err, data) {
responses++;
if (!success) {
if(func(err,data)){
success = true;
done(err, data);
}
else if(responses >= length){ //we have received all responses, but none met the condition
success = true; // probably should rename success var, but ok for now
done(new Error('all responses received, but no responses met condition'), data);
}
else{
//do nothing, wait for the next response
}
}
});
}
}; |
so that might look like this in action: async.first([], function(err,data){
if(err){
return false;
}
else if(data.error){
return false;
}
else{
return true;
}
}, function done(err, data){
// do your thing
}); |
I think |
I thought about that name too (or maybe some other mention of race seeded my thought process) |
I implemented an asyncOneOf / asyncOneOfParallel because I liked this as well. |
Closed by #1038 ! |
😸 👏 |
I found myself needing this type of function a few times:
done
whenever the first one finished, and ignore any subsequent resultsIt's easily implemented as follows, but does it belong in
async
? Happy to submit a PR with unit tests.The text was updated successfully, but these errors were encountered: