【发布时间】:2021-04-23 19:21:02
【问题描述】:
我遇到了一个问题,我的 Promise.all 解决得太早了。对于测试,我想 console.log 从 promise 映射中推送的数组的长度,但遗憾的是它返回 0。我敢肯定这很简单......
fromPath(job.data.path, { density: 100, format: "png" }).bulk(-1, true).then(output => {
Promise.all(output.map(page =>
jimp.read(Buffer.from(page.base64, 'base64')).then(img =>
{
img.invert().getBase64Async(jimp.AUTO).then(data => imageArray.push(data.replace('data:image/png;base64,', ''))).catch(err => console.log(err))
}
).catch(err => console.log(err))
)).catch(err => console.log(err))
}
// This returns early
).then(console.log(imageArray.length)).then(done()).catch(err => console.log(err));
任何帮助将不胜感激。
【问题讨论】:
-
您的
then回调没有返回任何内容。你应该返回Promise.all(),你应该返回img.invert()..... -
这是一个错字。你忘记在它所在的
.then处理程序中返回来自img.invert().getBase64Async的承诺,所以来自.then的承诺不会等待来自getBase64Async的承诺。 (还有上面的 Promise.all,直接调用console.log并将其返回值传递给.then,然后...)
标签: javascript node.js promise