对于下面发布的代码,我收集了两个数组
Promises
和promisesTS1
中的promisesTS2
。我希望稍后当我调用或执行类似以下内容时执行收集的承诺:
individualBandsDataForPathForTS1.map(async data => {
data.map(load => {
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;
}
})
})
})
individualBandsDataForPathForTS2.map(async data => {
data.map(load => {
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;
}
})
});
someFunction
当您执行new Promise(someFunction)
时,它会立即被调用。对于匿名函数或由 Array.map
返回的匿名函数列表传递给 Promise.all
也是如此。当您 await
Promise 时,该代码的执行不会发生。你不能拖延他们的执行。如果你想控制它们的执行时间,那么不要构建一个 Promise 列表,而是构建一个函数列表,然后你可以 await Promise.all(unitsOfWork.map(unit => unit())
或类似的东西。
您到目前为止所执行的代码是因为您在嵌套的 .map() 调用中使用了await。要使用 Promise.all 获得延迟执行,您可以执行以下修改:
您可以从处理各个组合的函数 .map() 中删除等待。这样就可以在不等待每个承诺解决的情况下创建承诺。 2. 退货承诺:
不要将已解析的数据强制放入数组中,而是更改为从 .map() 调用返回新的 Promise。在处理每个组合物后,这些应该解析为所需的数据。 3. 更新外部 .map() 调用 在外部 .map() 调用中,更改回使用普通函数而不是异步函数。这样,承诺就可以被收集,而无需为每个循环运行。
const promisesTS1 = [];
const promisesTS2 = [];
Promise.all([
individualBandsDataForPathForTS1.map(data => {
return data.map(load => {
return load.map(composition => {
switch (composition.band) {
case SysConstants.CONST_BAND_1_RAW_NAME_IS_AVERAGES.description:
return 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,
});
});
case SysConstants.CONST_BAND_2_RAW_NAME_IS_NUM_OF_CLOUD_FREE_SAMPLES.description:
return new Promise((resolve, reject) => {
// ts2Compositions[1] = composition;
sorted = composition.contents.sort((a, b) => a - b);
thresholdsInfoForBand2ForTS1 = {
band2_minMaxNDVI_TS1: MathUtils.findMinMax('band_2_ndvis_averages_MinMaxGeoTIFF_TS1_', composition.contents),
band2_percentile10_TS1: MathUtils.findPercentile('band_2_ndvis_averages_P10_GeoTIFF_TS1_', sorted, .1).percentile,
band2_percentile90_TS1: MathUtils.findPercentile('band_2_ndvis_averages_P90_TIFF_TS1_', sorted, .9).percentile,
};
resolve({
compositionsOfBand2ForTS1: composition,
sorted: sorted,
thresholdsInfoForBand2ForTS1,
});
});
default:
return Promise.resolve(null); // Handle other cases if needed
}
});
});
}),
individualBandsDataForPathForTS2.map(data => {
return data.map(load => {
return load.map(composition => {
// Similar logic for TS2 data processing as TS1
// ...
});
});
}),
])
.then(allPromises => { // This then block will be executed later
// allPromises will be an array containing all resolved promises
const processedDataTS1 = allPromises[0]; // Array of results from TS1 processing
const processedDataTS2 = allPromises[1]; // Array of results from TS2 processing
// Now you can access and use the processed data from each timeseries
// ...
});