Execute batch of promises in series. Once Promise.all is done go to the next batch

后端 未结 6 1868
星月不相逢
星月不相逢 2021-02-19 15:57

I have an array that contains an array of promises, and each inner array could have either 4k, 2k or 500 promises.

In total there are around 60k promises and I may test

6条回答
  •  南方客
    南方客 (楼主)
    2021-02-19 16:36

    You can do it recursively, for example here I needed to put about 60k documents in mongo, but it was too big, to do it in one step, therefore I take 1k documents, send them to the mongo, after it is finished I take another 1k documents etc.

    exports.rawRecursive = (arr, start) => {
            //ending condition
            if (start > arr.length) {
                return;
            }
    
            Rawmedicament.insertManyAsync(_.slice(arr, start, start + 1000)).then(() => {
                //recursive
                exports.rawRecursive(arr, start + 1000);
            });
    };
    

    If you want to notice, when everything is done, you can in ending condition put callback or if you like Promises you can call resolve() there.

提交回复
热议问题