mbacvanski mbacvanski - 1 year ago 104
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

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

Answer Source

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

  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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download