Created
April 28, 2016 20:49
-
-
Save luizamboni/7c2e662e5b3ebe5a8947f94a57bcea28 to your computer and use it in GitHub Desktop.
like a Thread pool in nodejs
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
"use strict"; | |
var P = require("bluebird") | |
var _ = require("underscore") | |
let items = [0,12.5,5,5,87,8,7,48,58,7,78,4,87,854,84,787,78,4,87,7] | |
console.log("items length ------------> " + items.length) | |
let level = 0 | |
function processList(_items){ | |
level++ | |
console.log("level ------------> " + level) | |
console.log(_items) | |
let selecteds = [] | |
for(var counter=0;counter < 3; counter++) | |
{ | |
if(_items.length > 0){ | |
selecteds.push(anyAsyncMethod(_items.pop())) | |
} | |
} | |
console.log("promisses count -> " + selecteds.length) | |
P.all(selecteds) | |
.then( results => { | |
if(_items.length > 0){ | |
processList(_items) | |
}else{ | |
console.log("---------> END") | |
process.exit(0) | |
} | |
}) | |
} | |
processList(items) | |
function anyAsyncMethod(number){ | |
let rand = Math.round(Math.random() * (3000 - 500)) + 500; | |
let p = new Promise(function(resolve){ | |
setTimeout(function(){ | |
console.log("resolving " + number) | |
resolve(number) | |
}, rand) | |
}); | |
return p | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment