I want something like the async
library to do two parallel calls either one succeeds in javascript and then the callback to operate on the succeeded output. It does not seem that async
has that. parallel
would err out if either one fails.
In general, if I have N
tasks, and I want to guarantee m
succeed and use the output from these m
calls, how can I do it?
You can use jQuery.Deferred and jQuery.when to achieve this (there are other promise libraries too).
jQuery.when: https://api.jquery.com/jQuery.when/
jQuery.Deferred: https://api.jquery.com/category/deferred-object/
Promise.race
You want
Promise.race
(assuming you are willing to move fromasync
to promises):From MDN:
Writing
race
in callback fashionAs far as I can see
async
does not provide the equivalent of race, but you can do something like:Perhaps you want a variant of
race
where errors are ignored--although that design seems questionable. In that case:Return first n non-error cases: