1 回答

TA贡献1831条经验 获得超9个赞
遗憾的是,没有安全的方法来将回调与 结合使用。最简单的解决方案是以如下所示的方式重新实现。请注意,它本身会返回一个 promise,因此您需要使用 来继续您的程序。asyncforEachforEachasyncasyncForEach.then()
const asyncForEach = async (array, callback) => {
try {
for (let i = 0; i < array.length; i++) {
await callback(array[i]);
}
}
catch (ex) {
// handle exception here.
}
}
asyncForEach(array, async () => {
let bom = await BOMChild.findOne({
where: { id: e.id },
});
if (bom.BOMProductId || bom.BOMProductId === 0) {
throw {
name: " error",
description: `Description `,
};
}
}).then(() => {
// continue program.
});
如果您想使用更干净但更令人困惑的解决方案来测试您的承诺印章,请看一下.Promise.all()
添加回答
举报