Promise.all()不在等待中

时间:2019-10-29 22:44:26

标签: javascript node.js server promise

我正在尝试从包含值数组的服务器发送响应。此值数组是使用await Promise.all生成的。但是由于某种原因,服务器不会等到所有promise都解决后再发送结果,所以我从服务器获取了未定义的值。该代码最终得到解决,因为条目已保存在数据库中,并且文件已按预期重命名。

if(desktopImage){
        desktopImgResponse = await Promise.all(desktopImage.map(catchAsync(async (img) => {
            const newImg = await Image.create({...});
            await fs.rename(`client/public/fs/projects/${img.filename}`, `client/public/fs/projects/${newImg.file}`, (err)=>{
                if (err) throw err;
            });
            return newImg;
        })));
    }

res.status(201).json({
        status: 'success',
        data: {
            desktopImages: desktopImgResponse
        }
    })

0 个答案:

没有答案
相关问题