如何收集所有 Promise 并稍后使用 Promise.all 执行它们

问题描述 投票:0回答:1

对于下面发布的代码,我收集了两个数组

Promises
promisesTS1
中的
promisesTS2
。我希望稍后当我调用或执行类似以下内容时执行收集的承诺:

.then(async () => {
    await Promise.all([
        Promise.all(promisesTS1),
        Promise.all(promisesTS2),
    ]);
});

但是在运行时发生的情况是,代码正常执行,例如我可以访问

thresholdsInfoForBand1ForTS1
。 我想知道如何修改下面发布的代码,以便将承诺收集在上面/之前提到的相应数组中,然后在调用时执行收集的承诺
Promise.all

请问如何实现? 代码

await Promise.all([
    Promise.all(individualBandsDataForPathForTS1.map(async data => {
        return await Promise.all(data.map(load => {
            return Promise.all(load.map(composition => {
                switch (composition.band) {
                    case SysConstants.CONST_BAND_1_RAW_NAME_IS_AVERAGES.description:
                        promisesTS1.push(new Promise((resolve, reject) => {
                            // ts1Compositions[0] = composition;
                            sorted = composition.contents.sort((a, b) => a - b);
                            thresholdsInfoForBand1ForTS1 = {
                                band1_minMaxNDVI_TS1: MathUtils.findMinMax('band_1_ndvis_averages_MinMaxGeoTIFF_TS1_', composition.contents),
                                band1_percentile10_TS1: MathUtils.findPercentile('band_1_ndvis_averages_P10_GeoTIFF_TS1_', sorted, .1).percentile,
                                band1_percentile90_TS1: MathUtils.findPercentile('band_1_ndvis_averages_P90_GeoTIFF_TS1_', sorted, .9).percentile,
                            };
                            resolve({
                                compositionsOfBand1ForTS1: composition,
                                sorted: sorted,
                                thresholdsInfoForBand1ForTS1,
                            });
                        }).then(() => {
                            compositionsOfTS1.push(composition);
                        })
                        );
                        break;
                    case SysConstants.CONST_BAND_2_RAW_NAME_IS_NUM_OF_CLOUD_FREE_SAMPLES.description:
                        promisesTS1.push(new Promise((resolve, reject) => {
                            // ts1Compositions[1] = composition;
                            sorted = composition.contents.sort((a, b) => a - b);
                            thresholdsInfoForBand2ForTS1 = {
                                band2_minMaxNDVI_TS1: MathUtils.findMinMax('band_2_num_of_cloud_free_samples_MinMaxGeoTIFF_TS1_', composition.contents),
                                band2_percentile10_TS1: MathUtils.findPercentile('band_2_num_of_cloud_free_samples_P10_GeoTIFF_TS1_', sorted, .1).percentile,
                                band2_percentile90_TS1: MathUtils.findPercentile('band_2_num_of_cloud_free_samples_P90_GeoTIFF_TS1_', sorted, .9).percentile,
                            };
                            resolve({
                                compositionsOfBand2ForTS1: composition,
                                sorted: sorted,
                                thresholdsInfoForBand2ForTS1,
                            });
                        }).then(() => {
                            compositionsOfTS1.push(composition);
                        })
                        );
                        break;
                }
            }));
        }));
    })),
    Promise.all(individualBandsDataForPathForTS2.map(async data => {
        return await Promise.all(data.map(load => {
            return Promise.all(load.map((composition) => {
                switch (composition.band) {
                    case SysConstants.CONST_BAND_1_RAW_NAME_IS_AVERAGES.description:
                        promisesTS2.push(new Promise((resolve, reject) => {
                            // ts2Compositions[0] = composition;
                            sorted = composition.contents.sort((a, b) => a - b);
                            thresholdsInfoForBand1ForTS2 = {
                                band1_minMaxNDVI_TS2: MathUtils.findMinMax('band_1_ndvis_averages_MinMaxGeoTIFF_TS2_', composition.contents),
                                band1_percentile10_TS2: MathUtils.findPercentile('band_1_ndvis_averages_P10_GeoTIFF_TS2_', sorted, .1).percentile,
                                band1_percentile90_TS2: MathUtils.findPercentile('band_1_ndvis_averages_P90_GeoTIFF_TS2_', sorted, .9).percentile,
                            };
                            resolve({
                                compositionsOfBand1ForTS2: composition,
                                sorted: sorted,
                                thresholdsInfoForBand1ForTS2,
                            });
                        }).then(() => {
                            compositionsOfTS2.push(composition);
                        })
                        );
                        break;
                    case SysConstants.CONST_BAND_2_RAW_NAME_IS_NUM_OF_CLOUD_FREE_SAMPLES.description:
                        promisesTS2.push(new Promise((resolve, reject) => {
                            // ts2Compositions[1] = composition;
                            sorted = composition.contents.sort((a, b) => a - b);
                            thresholdsInfoForBand2ForTS2 = {
                                band2_minMaxNDVI_TS2: MathUtils.findMinMax('band_2_ndvis_averages_MinMaxGeoTIFF_TS2_', composition.contents),
                                band2_percentile10_TS2: MathUtils.findPercentile('band_2_ndvis_averages_P10_GeoTIFF_TS2_', sorted, .1).percentile,
                                band2_percentile90_TS2: MathUtils.findPercentile('band_2_ndvis_averages_P90_GeoTIFF_TS2_', sorted, .9).percentile,
                            };
                            resolve({
                                compositionsOfBand2ForTS2: composition,
                                sorted: sorted,
                                thresholdsInfoForBand2ForTS2,
                            });
                        }).then(() => {
                            compositionsOfTS2.push(composition);
                        })
                        );
                        break;
                }
            }));
        }));
    }))
]);
javascript node.js promise
1个回答
0
投票

someFunction
当您执行
new Promise(someFunction)
时,它会立即被调用。对于匿名函数或由
Array.map
返回的匿名函数列表传递给
Promise.all
也是如此。当您
await
Promise 时,该代码的执行不会发生。你不能拖延他们的执行。如果你想控制它们的执行时间,那么不要构建一个 Promise 列表,而是构建一个函数列表,然后你可以
await Promise.all(unitsOfWork.map(unit => unit())
或类似的东西。

© www.soinside.com 2019 - 2024. All rights reserved.