我正在尝试在代码中使用async
和await
函数,但发现很难做到。我要实现的目标是,有一个名为getIds
的函数,该函数总是有可能成为循环,直到该函数完成,下一个代码块才应该运行
getIds() {
for (let r = 0; r < this.grade.length; r++) {
this.idservice.getIds(this.grade[r])
.subscribe(
response => {
if (response && response.code === HttpStatus.OK) {
for (let r = 0; r < response.data.length; r++) {
this.newIds.push({
id: response.data[r].id
})
}
console.log(this.studentIds)
}
}
);
}
}
现在这是调用getIds函数的函数
createMenu() {
this.getIds();
//till getIds finishes code underneath shouldn't run
this.idservice.addNew(this.newIds)
.subscribe(
response => {
if (response && response.code === HttpStatus.CREATED) {
console.log(JSON.stringify(response.data));
}
}
);
}