DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at codedrops.tech

Promise.race()

Returns the first resolved promise from the list.

const getAnyItem = async (itemId1, itemId2) => {
  return Promise.race([
    fetch(`https://jsonplaceholder.typicode.com/albums/${itemId1}`),
    fetch(`https://jsonplaceholder.typicode.com/albums/${itemId2}`),
  ]).then((item) => {
    // ...
  });
};
getAnyItem(2, 5);
Enter fullscreen mode Exit fullscreen mode

Thanks for reading ๐Ÿ’™

Follow @codedrops.tech for daily posts.

Instagram โ— Twitter โ— Facebook

Micro-Learning โ— Web Development โ— Javascript โ— MERN stack โ— Javascript

codedrops.tech

Top comments (1)

Collapse
 
jonyk56 profile image
Jonyk56

poggers, this is useful as heck xD