Skip to content

Instantly share code, notes, and snippets.

@llimacruz
Created May 31, 2017 01:07
Show Gist options
  • Save llimacruz/62521f82ed03cf02d44eda8f827011a4 to your computer and use it in GitHub Desktop.
Save llimacruz/62521f82ed03cf02d44eda8f827011a4 to your computer and use it in GitHub Desktop.
Exemplo de utilização de async.queue mais async.whilst
var async = require('async')
var tempoEsperaSemRegistro = 20 * 1000
var quantidadeParalela = 10
var quantidadeFila = 100
var count = 0;
var cb;
var encheFila = (callback) => {
if (callback) {
console.log(callback)
cb = callback
}
//var fila = [];
if (count == 3) {
count = 0;
cb();
} else {
console.log('encher fila')
for (var i = 0; i < quantidadeFila; i++) {
q.push(i)
}
count++;
}
}
// create a queue object with concurrency 2
var q = async.queue(function(task, callback) {
console.log('hello ' + task);
setTimeout(callback, 100);
}, quantidadeParalela);
// assign a callback
q.drain = function() {
console.log('all items have been processed');
encheFila();
};
async.whilst(
function() { return true },
function(callback) {
console.log('chamar queue')
encheFila(() => {
console.log('voltou')
setTimeout(()=>{
console.log('nada para processar, aguardar para buscar mais')
callback()
},
tempoEsperaSemRegistro);
})
},
function (err, n) {
console.log('err', err)
console.log('err', n)
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment