Skip to content

Instantly share code, notes, and snippets.

@alanhoff
Created June 8, 2017 02:42
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 alanhoff/879522c1af431db75914a20f349c9918 to your computer and use it in GitHub Desktop.
Save alanhoff/879522c1af431db75914a20f349c9918 to your computer and use it in GitHub Desktop.
var semaforo = 0
var valores = ['hello', 'world']
function callback () {
semaforo--
if (semaforo === 0) {
console.log('Acabou tudo o que tinha pra fazer, vou finalizar')
process.exit()
} else {
console.log('Ainda tem trabalho pra fazer')
}
}
valores.forEach(function (valor) {
// Eu indico a quantidade de trabalho que está pendende
// ao incrementar o semáforo
semaforo++
// Esse setTimeout serve apenas para emular uma função
// assíncrona neste exemplo
setTimeout (callback, 500)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment