Skip to content

Instantly share code, notes, and snippets.

@srebalaji
Created March 4, 2019 16:57
Show Gist options
  • Select an option

  • Save srebalaji/b4699e16767f5907d7216d26d71abf41 to your computer and use it in GitHub Desktop.

Select an option

Save srebalaji/b4699e16767f5907d7216d26d71abf41 to your computer and use it in GitHub Desktop.
A simple JS file that explains Promise.all
// A simple promise that resolves after a given time
const timeOut = (t) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Completed in ${t}`)
}, t)
})
}
// Resolving a normal promise.
timeOut(1000)
.then(result => console.log(result)) // Completed in 1000
// Promise.all
Promise.all([timeOut(1000), timeOut(2000)])
.then(result => console.log(result)) // ["Completed in 1000", "Completed in 2000"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment