Javascript 异步/等待循环数组。推送问题
我目前正在做一些事情,需要获取异步数据并将其推送到数组中。请看下面Javascript 异步/等待循环数组。推送问题,javascript,arrays,async-await,for-of-loop,Javascript,Arrays,Async Await,For Of Loop,我目前正在做一些事情,需要获取异步数据并将其推送到数组中。请看下面 const getStudent = async (id: string) => { const student = await fetchSomeData(id); const markedEssayArray: string[] = []; for await(const essayName of student.essay) { const markedEssay = await fetc
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,并用标记的版本更新student.article数组。但是,当我测试代码时,student.artison数组返回一个空数组[]
谁能告诉我哪里出了问题吗?你可以这样尝试:
删除
在之后等待for
=>for(student.assessment的const-essayName)
什么是student.assessment
?承诺数组?我已经尝试删除了wait after for,但不幸的是仍然得到了相同的结果。对于另一个问题,student.attraction是一个字符串数组,您是否将markedEssay
记录在for..of
循环中?它是否打印出您预期的结果?
const getStudent = async (id: string) => {
const student = await fetchSomeData(id);
let markedEssayArray: string[] = [];
const essayArr = student.essay;
essayArr.forEach((singleEssay) => {
let markedEssay = await fetchMarkedEssay(essayName);
markedEssayArray.push(markedEssay)
}
student.essay = [...markedEssayArray];
return student;
};