Skip to content

Instantly share code, notes, and snippets.

@webarthur
Last active September 11, 2017 14:39
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 webarthur/9898d04a7bd9854e5613a10da9b007b3 to your computer and use it in GitHub Desktop.
Save webarthur/9898d04a7bd9854e5613a10da9b007b3 to your computer and use it in GitHub Desktop.
Uso de Promise de forma paralela e serial com async/await
// 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