Created
August 10, 2024 14:44
-
-
Save MarwanShehata/fbd16f7bebf91f426813018943e3003d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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