【发布时间】:2021-07-17 07:10:29
【问题描述】:
我目前正在做一些我需要获取异步数据并将其推送到数组的事情。请看下文。
const getStudent = async (id: string) => {
const student = await fetchSomeData(id);
const markedEssayArray: string[] = [];
for await(const essayName of student.essay) {
const markedEssay = await fetchMarkedEssay(essayName);
markedEssayArray.push(markedEssay)
};
student.essay = markedEssayArray;
return student;
};
所以基本上,我获取一个学生,并用标记的版本更新 student.essay 数组。然而,当我测试代码时,student.essay 数组返回一个空数组 []。
谁能告诉我哪里出错了?
【问题讨论】:
-
在
for之后删除await=>for(const essayName of student.essay) -
student.essay是什么?承诺数组? -
我尝试在 for 之后删除 await,但不幸的是仍然得到相同的结果。对于另一个问题,student.essay 是一个字符串数组
-
您是否将
markedEssay登录到for..of循环中?它会打印出您预期的结果吗?
标签: javascript arrays async-await for-of-loop