Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const promiseWillFulfill = [
new Promise((resolve, reject) => setTimeout(reject, 250, '๐Ÿ˜ˆ')),
new Promise((resolve, reject) => setTimeout(resolve, 150, '๐Ÿ˜‡')),
new Promise((resolve, reject) => setTimeout(resolve, 1, '๐Ÿ˜‡')),
]
Promise.race(promiseWillFulfill)
.then(value => console.log(`The humanity survives "${value}"`))
.catch(error => console.log(`Won't be called as ๐Ÿ˜‡ will win the race`))
const promiseWillReject = [
new Promise((resolve, reject) => setTimeout(resolve, 250, '๐Ÿ˜‡')),
new Promise((resolve, reject) => setTimeout(reject, 1, '๐Ÿ˜ˆ')),
new Promise((resolve, reject) => setTimeout(resolve, 250, '๐Ÿ˜‡')),
]
Promise.race(promiseWillReject)
.then(value => console.log(`This won't be called...="${value}"`))
.catch(error => console.log(`The humanity is doomed...="${error}"`))
const promisesWithOUTReject = [
new Promise(resolve => setTimeout(resolve, 350, 'one')),
new Promise(resolve => setTimeout(resolve, 250, 'two')),
new Promise(resolve => setTimeout(resolve, 150, 'three')),
]
Promise.race(promisesWithOUTReject)
.then(value => console.log(`Promise without reject="${value}"`))
The humanity survives "๐Ÿ˜‡"
The humanity is doomed...="๐Ÿ˜ˆ"
Promise without reject="three"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You canโ€™t perform that action at this time.