mbacvanski mbacvanski - 1 month ago 6
Node.js Question

Node.js Get First Resolved Promise

If I have two promises A and B, only one of which will succeed, how can I get whichever one resolves successfully? I'm looking for something similar to

Promise.race
, but which will return only the first promise that resolves. I'm using promises from ES6.

Answer

I would handle this by using Promise.race, but have some code to ignore the errors. Untested but try something like this...

Promise.race([
  promiseA.catch((e) => {}),
  promiseB.catch((e) => {})
])

Note that you might have to give some thought to how you handle cases where they all error out.