Skip to content

Instantly share code, notes, and snippets.

@zhoufenfens
Last active August 7, 2020 17:18
Show Gist options
  • Save zhoufenfens/b335fcfadff9d628103262d6fa1dd48a to your computer and use it in GitHub Desktop.
Save zhoufenfens/b335fcfadff9d628103262d6fa1dd48a to your computer and use it in GitHub Desktop.
尽快输出
const asyncFun = (i, cb) => {
setTimeout(() => {
let result = 2 * i
cb(result)
}, Math.random() * 1000);
}
const results = []
var rIndex = 1
const handle = (result, i) => {
results[i] = result
while (results[rIndex]) { // (results[rIndex]) 不能有0正常值
console.log(rIndex + "," + results[rIndex++]);
}
}
var total = 20;
for (let i = 1; i < total; i++) {
((j) => {
asyncFun(j, (result) => {
handle(result, j)
})
})(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment