Skip to content

Instantly share code, notes, and snippets.

@MarwanShehata
Created August 10, 2024 14:44
Show Gist options
  • Save MarwanShehata/fbd16f7bebf91f426813018943e3003d to your computer and use it in GitHub Desktop.
Save MarwanShehata/fbd16f7bebf91f426813018943e3003d to your computer and use it in GitHub Desktop.
function slow(id, cb) {
setTimeout(function () {
cb(null, id)
}, Math.random() * 3000)
}
let promises = []
for (let i = 0; i < 3; i++) {
const prom = new Promise((resolve, reject) => {
slow(i, function (err, res) {
if (err) reject(err)
else resolve(res)
})
promises.push(prom)
})
}
Promise.all(promises)
.then((res) => {
console.log(res)
})
.catch((err) => {
console.error(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment