await 不会暂停执行,直到完成[关闭]

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

对于下面发布的代码,尽管我在

await
之前使用
Promise.all
,但代码的执行不会等到
await Promise.all
首先完成。 发生的情况是,代码包含:

.then(async () => {
        let averagesNDVIs = null;
        let sorted = null;
        let minMaxNDVI = null;
        ......
        ......
        ......

总是先被执行。

请让我知道为什么会发生这种情况以及如何解决此问题

代码

.then(async (resp) => {
    bandsDataForTS1 = resp[0];
    bandsDataForTS2 = resp[1];

    await Promise.all([
        ...bandsDataForTS1.map((data, index) => {
            IOUtils.writeForPath(
                titlesOfBandsAsGeoTIFFForTS1[index],
                IOResources.CONST_OUTPUT_PATH_FOR_TIFFS.description,
                data.contents,
                '.tiff',
            )
            .then(ioResults => {
                processAPIBandsAsGeoTIFFFileNamesForTS1.push(ioResults.fileName);
                processAPIBandsAsGeoTIFFFullPathsForTS1.push(ioResults.fullPath);
                this.#msg = ({msg:infoTag + 'WS:/*************************************++WSSentinelhubProcessAPINDVIsProcessor/ tiff-contents are written to file successfully.', 
                    processAPIPathOfBandsDataAsGeoTIFFFForTS1: ioResults.fullPath,
                    processAPIFileNameOFBandsDataAsGeoTIFFForTS1: ioResults.fileName,
                });
                console.info(this.#msg);
            })
            .catch(e => {
                this.#msg = ({msg:infoTag + 'WS:/WSSentinelhubProcessAPINDVIsProcessor/ tiff-contents failed to be written to file.', 
                    processAPIPathOfBandsDataAsGeoTIFFFForTS1: ioResults.fullPath,
                    processAPIFileNameOFBandsDataAsGeoTIFFForTS1: ioResults.fileName,
                });
                console.info(this.#msg);
                throw new Error(this.#msg);
            });
        }),
        
        ...bandsDataForTS2.map((data, index) => {
            IOUtils.writeForPath(
                titlesOfBandsAsGeoTIFFForTS2[index],
                IOResources.CONST_OUTPUT_PATH_FOR_TIFFS.description,
                data.contents,
                '.tiff',
            )
            .then(ioResults => {
                processAPIBandsAsGeoTIFFFileNamesForTS2.push(ioResults.fileName);
                processAPIBandsAsGeoTIFFFullPathsForTS2.push(ioResults.fullPath);
                this.#msg = ({msg:infoTag + 'WS:/WSSentinelhubProcessAPINDVIsProcessor/ tiff-contents are written to file successfully.', 
                    processAPIPathOfBandsDataAsGeoTIFFFForTS2: ioResults.fullPath,
                    processAPIFileNameOFBandsDataAsGeoTIFFForTS2: ioResults.fileName,
                });
                console.info(this.#msg);
            })
            .catch(e => {
                this.#msg = ({msg:infoTag + 'WS:/WSSentinelhubProcessAPINDVIsProcessor/ tiff-contents failed to be written to file.', 
                    processAPIPathOfBandsDataAsGeoTIFFFForTS2: ioResults.fullPath,
                    processAPIFileNameOFBandsDataAsGeoTIFFForTS2: ioResults.fileName,
                });
                console.info(this.#msg);
                throw new Error(this.#msg);
            });
        }),
    ])
    .then(async () => {
        let averagesNDVIs = null;
        let sorted = null;
        let minMaxNDVI = null;
        //to note/comment: the element of index [0] is the one of interest, as it contains the averages vlaues of ndvis. The bands are in this order:[averages, numOfCloudFreeSamples, numOfCloudySamples, percentageOfCloudFree, percentageOfCloudy];
        averagesNDVIs = bandsDataForTS1[0].contents;
        sorted = averagesNDVIs.sort((a, b) => a-b);
        minMaxNDVI = MathUtils.findMinMax('_NDVI_averages_GeoTIFF_TS1_', averagesNDVIs);
        percentile10ForTS1 = MathUtils.findPercentile('_NDVI_averages_GeoTIFF_TS1_', sorted, .1).percentile;
        percentile90ForTS1 = MathUtils.findPercentile('_NDVI_averages_GeoTIFF_TS1_', sorted, .9).percentile;
        minNDVIForTS1 = minMaxNDVI.min;
        maxNDVIForTS1 = minMaxNDVI.max;

        //to note/comment: the element of index [0] is the one of interest, as it contains the averages vlaues of ndvis. The bands are in this order:[averages, numOfCloudFreeSamples, numOfCloudySamples, percentageOfCloudFree, percentageOfCloudy];
        averagesNDVIs = bandsDataForTS2[0].contents;
        sorted = averagesNDVIs.sort((a, b) => a-b);
        minMaxNDVI = MathUtils.findMinMax('_NDVI_averages_GeoTIFF_TS2_', averagesNDVIs);
        percentile10ForTS2 = MathUtils.findPercentile('_NDVI_averages_GeoTIFF_TS2_', sorted, .1);
        percentile90ForTS2 = MathUtils.findPercentile('_NDVI_averages_GeoTIFF_TS2_', sorted, .9);
        minNDVIForTS2 = minMaxNDVI.min;
        maxNDVIForTS2 = minMaxNDVI.max;
    });                            
})
javascript node.js promise
1个回答
0
投票

您的地图回调不会重新调整任何内容:

await Promise.all([
    ...bandsDataForTS1.map((data, index) => {
        // you are not returning anything here!
        IOUtils.writeForPath(
            titlesOfBandsAsGeoTIFFForTS1[index],
            IOResources.CONST_OUTPUT_PATH_FOR_TIFFS.description,
            data.contents,
            ".tiff"
        )
            .then((ioResults) => { ... })
            .catch((e) => {});
    }),
    ...
]);

第二个数组也一样。

快速解决方案:从回调中返回。

更强大的解决方案:重构代码以使用更多函数,并将

.then()
实例替换为
async
/
await

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