The keyword await is used to wait for a Promise. It can only be used inside an async function. This keyword makes JavaScript wait until that promise settles and returns its result. Here is an example with a promise that resolves in 2 seconds.
To resolve the promise, we called the then() method on the promise object. The function we passed to the then() method gets called with the resolved value as a parameter. We can access this value in the body of the function. Notice that we also used the catch() method.
race() , which returns the first settled value (either fulfillment or rejection), this method returns the first fulfilled value.
The fulfilled value is an empty array. If a nonempty iterable is passed, and all of the promises fulfill, or are not promises, then the promise returned by this method is fulfilled asynchronously.
If I have two promises A and B, only one of which will succeed, how can I get whichever one fulfills successfully? I'm looking for something similar to Promise.race
, but which will return only the first promise that fulfills. I'm using promises from ES6.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With