Skip to content

Instantly share code, notes, and snippets.

@dance2die
Created May 18, 2019 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dance2die/e59a0f90b6a0ea585520b994652bfb55 to your computer and use it in GitHub Desktop.
Save dance2die/e59a0f90b6a0ea585520b994652bfb55 to your computer and use it in GitHub Desktop.
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