Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript/TypeScript - loop of promises doesn't wait

I have a loop of 100 promises. I want to process 10 of those simultaneously, then print a statement, and continue with the next 10 in parallel. However, it does not wait for every 10 promises to finish their execution.

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);
       });
   }

My expectation is something like:

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
...

However, it is chaotically mixed up. What am I doing wrong? Any help appreciated.

like image 674
phoebus Avatar asked Feb 17 '26 21:02

phoebus


1 Answers

Another solution is to use array reduce to chain your promises:

var groups = Array.from(new Array(10),(val,index) => 10*index);	// => [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
groups.reduce((m, o) => {
    return m.then(() => {		
        var group = Array.from(new Array(10), (val, index) => o + index + 1);  // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], ...
        var promises = group.map(i => this.web3.eth.getBlock(i).then(() => winston.info("Processing block " + i)) );
        return Promise.all(promises).then(() => winston.info("+++ Processed " + 10 + " blocks") );
    });
}, Promise.resolve(true));   

It just gives you the expected result:

enter image description here

like image 161
Faly Avatar answered Feb 20 '26 09:02

Faly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!