我有100个承诺的循环。我想同时处理其中的10个,然后打印一个语句,然后并行继续下一个10。但是,它不会等待每10个承诺完成执行。
const promises = [];
for (let i = 1; i <= 100; i ++) {
const blockPromise = this.web3.eth.getBlock(i).then((block: any) => {
winston.info("Processing block " + i);
}).catch((err: Error) => {
winston.error(err);
});
promises.push(blockPromise);
if (i % 10 === 0) {
Promise.all(promises).then(() => {
winston.info("+++ Processed " + 10 + " blocks");
}).catch((err: Error) => {
winston.error(err);
});
}
我的期望是:
Processing block 1
Processing block 3
Processing block 7
Processing block 2
Processing block 4
Processing block 5
Processing block 6
Processing block 9
Processing block 10
Processing block 8
+++ Processed 10 blocks
Processing block 12
...
但是,它混乱不堪。我究竟做错了什么?任何帮助表示赞赏。
答案 0 :(得分:2)
答案 1 :(得分:1)
既然你每次都试图处理10个承诺,那么你可以在每批10个之后await
或者那会失败吗?
var promises = []; // You are mutating it so better not be a const
var blockPromise = null;
for (let i = 1; i <= 100; i ++) {
blockPromise = this.web3.eth.getBlock(i).then((block: any) => {
winston.info("Processing block " + i);
}).catch((err: Error) => {
winston.error(err);
});
promises.push(blockPromise);
if (i % 10 === 0) {
await Promise.all(promises);
promises = [] // clear the processed promises as well
winston.info("+++ Processed " + 10 + " blocks");
}
}
答案 2 :(得分:1)
内部for循环的承诺搞砸了一切。您可以使用async和await,或者您需要将整个事务减少到一个承诺链:
let promise = Promise.resolve(), promises = [];
for( let i = 0; i <= 100; i++){
promises.push(this.web3.eth.getBlock(i).then((block: any) => {
winston.info("Processing block " + i);
}).catch((err: Error) => {
winston.error(err);
}));
if (i % 10 === 0){
promise = Promise.all(promises.concat(promise))
.then(_=>winston.info("+++ Processed " + 10 + " blocks");
promises = [];
}
}