1 回答

TA貢獻1831條經(jīng)驗 獲得超9個贊
遺憾的是,沒有安全的方法來將回調(diào)與 結(jié)合使用。最簡單的解決方案是以如下所示的方式重新實現(xiàn)。請注意,它本身會返回一個 promise,因此您需要使用 來繼續(xù)您的程序。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()
添加回答
舉報