Last active
September 11, 2017 14:39
-
-
Save webarthur/9898d04a7bd9854e5613a10da9b007b3 to your computer and use it in GitHub Desktop.
Uso de Promise de forma paralela e serial com async/await
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
// exemplo de como seria com VanillaJS | |
// comportamento muda qndo há um await dentro da função | |
// com await funciona de forma assíncrona/paralela, sem await o loop se comporta de maneira síncrona/serial | |
var dados = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | |
// paralelo | |
// equivalente a async.map() | |
var i = 0 | |
Promise.all(dados.map(async function (item) { | |
i += 1 | |
await Promise.resolve(true) | |
console.log('paralelo', i, item) | |
return true | |
})).then(function () { | |
console.log(1) | |
}) | |
// serial | |
// equivalente a async.series() | |
var i = 0 | |
Promise.all(dados.map(function (item) { | |
i += 1 | |
console.log('serial', i, item) | |
return true | |
})).then(function () { | |
console.log(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment