Skip to content

Instantly share code, notes, and snippets.

@codephi
Last active May 16, 2017 14:42
Show Gist options
  • Save codephi/fd0d2c9528884760ae9d71bea5ff7f01 to your computer and use it in GitHub Desktop.
Save codephi/fd0d2c9528884760ae9d71bea5ff7f01 to your computer and use it in GitHub Desktop.
O resultado final deve ser 550, independente o index da iteração. Online test: https://runkit.com/philippeassis/591b0f3ae1482900125159b9
var Promise = require("promise-with-queue"),
val = 0, position = 0;
var loop = (index, callback) => {
setTimeout(() => {
val += index * 10;
console.log("loop | index:", index, "val:", val)
++position;
callback(val)
}, Math.floor(Math.random() * (500 - 2000) + 500))
}
var one = new Promise((resolve, reject) => resolve(1))
var two = (base) => new Promise((resolve, reject) => {
var total = (base * 10);
for (let i = base; i <= total; i++) {
loop(i, value => {
if (position == total) {
console.log("passou: ", i, "position:", position);
resolve(value);
}
})
}
})
var tree = (data) => new Promise((resolve, reject) => resolve(`Resultado: ${data}`))
new Promise.queue([one, two, tree]).then(data => console.log(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment