简体   繁体   English

如何在循环错误中修复意外的“await”

[英]How to fix Unexpected `await` inside a loop error

Eslint gives me an error "Unexpected await inside a loop". Eslint 给我一个错误“在循环中意外await ”。 I understand the problem, but can't find a way to fix the code.我明白这个问题,但找不到修复代码的方法。

I've read the eslint docs: https://eslint.org/docs/rules/no-await-in-loop When I wrap my loop in /* eslint-disable no-await-in-loop */ it fixes the error.我已经阅读了 eslint 文档: https ://eslint.org/docs/rules/no-await-in-loop 当我将循环包装在/* eslint-disable no-await-in-loop */它修复了错误。 However, that is not efficient, because every promise will wait for previous one to finish.然而,这并不高效,因为每个 promise 都会等待前一个 promise 完成。

My promise results are not dependent on each other, so they could run concurrently.我的 promise 结果不相互依赖,因此它们可以同时运行。 Am I correct?我对么? I'd be glad if anyone could show me how to fix my current code and use Promises如果有人能告诉我如何修复我当前的代码并使用 Promises,我会很高兴

const resizeImages = async imagePaths => {
    try{

        // With the line bellow everything works as expected. But without it I get error
        /* eslint-disable no-await-in-loop */

        const results = [];

        for (const imagePath of imagePaths){
            const thumbDest = path.parse(imagePath).dir + '/resized' + path.basename(imagePath);
            let tempFilePath = path.join(os.tmpdir(), path.basename(imagePath));
            console.log("Image resizing in progress: " + imagePath);

            // File is downloaded to the firebase functions environment 
            await rootBucket.file(imagePath).download({
                destination: tempFilePath
            })
            // Uploading resized image back to the folder in firebase storage 
            await spawn('convert', [tempFilePath, '-resize', '900x900', tempFilePath]);
            // Uploading resized image back to the folder in firebase storage 
            const uploadedFile = await rootBucket.upload(tempFilePath, {
                destination: thumbDest,
                metadata: metadata
            })
            console.log("Resized img successfully saved in: " + thumbDest);
            console.log("Resized img mediaLink " + uploadedFile[0].metadata.mediaLink);

            results.push(uploadedFile[0].metadata.mediaLink);                        
        }

        /* eslint-enable no-await-in-loop */

        return resizedImages = results;
    }
    catch (err){
        return console.log("Function resizeImages error: " + err);
    }
}

That's part of a Google Firebase Cloud Function, where I try to create a thumbnail, reduce size of uploaded pictures and delete original ones.这是 Google Firebase Cloud Function 的一部分,我尝试在其中创建缩略图、减小上传图片的大小并删除原始图片。

   const results = await Promise.all(imagePaths.map(async imagePath => {
     // this now runs concurrently ...
     return uploadedFile[0].metadata.mediaLink;
   }));

Note that this is "not an error".请注意,这不是“错误”。 It's a warning that should help you improve your code.这是一个警告,应该可以帮助您改进代码。 in this case the different items can be processed in parallel, and that's what the warning wants to tell you.在这种情况下,可以并行处理不同的项目,这就是警告想要告诉您的。 There are situations were awaiting inside a loop makes totally sense though (eg if the processing needs to be done in order).不过,有些情况在循环内等待是完全合理的(例如,如果处理需要按顺序完成)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM