Skip to content

Instantly share code, notes, and snippets.

@hadnazzar
Created June 20, 2022 23:22
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 hadnazzar/6f682a821832e7ef0a43d19ec58ce4db to your computer and use it in GitHub Desktop.
Save hadnazzar/6f682a821832e7ef0a43d19ec58ce4db to your computer and use it in GitHub Desktop.
const promiseWithMilliseconds = (ms, index) => {
return new Promise((resolve,reject) => {
console.time(`Time:${index}`)
setTimeout(() => {
console.timeEnd(`Time:${index}`)
return resolve(`Resolved in ${ms}`)
}, ms);
})
}
const delays = [1000,2000,3000,4000]
const promisesToCall = delays.map((delay, index)=> {
return promiseWithMilliseconds(delay, index)
})
Promise.all(promisesToCall)
.then((result) => {
console.log('Result', result)
})
.catch((error) => console.log(`Error: ${error}`))
/*
Time:0: 1003.169921875 ms
Time:0: 1.005s
Time:1: 2003.0888671875 ms
Time:1: 2.004s
Time:2: 3000.85205078125 ms
Time:2: 3.001s
Time:3: 4001.341064453125 ms
Time:3: 4.002s
Result (4) ['Resolved in 1000', 'Resolved in 2000', 'Resolved in 3000', 'Resolved in 4000']
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment