【发布时间】:2021-10-23 05:52:15
【问题描述】:
在我的 reduce 循环中,我试图从一个数组中获取两个数组。我在迭代中有异步逻辑,并且我得到了一个错误,正如你所看到的,因为我在第三次迭代中得到了结果,并且破坏了我的脚本.在这里我附上我的代码来展示我是如何做到的!
const {
readyScoreRequests,
remainingScores,
} = await scoreRequests.reduce(async (result, score) => {
const { profile } = score;
console.log('result', result);
const importedScore = score.scoreId ? await importedScoreService.findOne({
where: { id: score.scoreId },
}) : null;
const scoreDate = importedScore ? moment(importedScore.createdOn).format('MM/DD/YYYY') : 'New Score';
const scoreInfo = {
status: score.status.charAt(0).toUpperCase() + score.status.slice(1),
scoreDate,
name: profile ? profile.name : null,
email: score.companyEmail ? score.companyEmail : null,
city: profile ? profile.city : null,
state: profile ? profile.state : null,
logo: profile ? profile.logo : null,
};
if (score.status === 'ready') {
result.readyScoreRequests.push(scoreInfo);
return result;
}
result.remainingScores.push(scoreInfo);
return result;
}, { readyScoreRequests: [], remainingScores: [] });
【问题讨论】:
标签: javascript node.js asynchronous promise reduce