Skip to content

Instantly share code, notes, and snippets.

@bobisme
Last active August 28, 2024 15:05
Show Gist options
  • Save bobisme/63c8fdb78a82692580bc31b277cf571e to your computer and use it in GitHub Desktop.
Save bobisme/63c8fdb78a82692580bc31b277cf571e to your computer and use it in GitHub Desktop.
Promise.race with error
(async () => {
const promiseA = new Promise((res) => setTimeout(() => res("yes"), 500));
const promiseB = new Promise((_, rej) => setTimeout(() => rej("fail"), 3000));
return await Promise.race([promiseA, promiseB]);
})()
.then((x) => console.log("val =", x))
.catch((err) => {
console.error("error here", err);
});
// val = yes will be logged after 500ms
// the process will still wait for 3 seconds, but no error will be thrown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment