Last active
August 28, 2024 15:05
-
-
Save bobisme/63c8fdb78a82692580bc31b277cf571e to your computer and use it in GitHub Desktop.
Promise.race with error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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