DEV Community

loading...

Promise.race()

318097 profile image Mehul Lakhanpal Originally published at codedrops.tech ・1 min read

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.

InstagramTwitterFacebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Discussion (1)

pic
Editor guide
Collapse
jonyk56 profile image
Jonyk56

poggers, this is useful as heck xD