问题
When I run the code below, the checkCard function runs before the getTimecard function is finished.
getTimecard() {
this._service.getTimecard().subscribe(
data => {
this.sending = true;
this.timecards = data;
},
err => {
this.sending = false;
console.error(err);
},
() => {
this.sending = false;
}
);
}
checkCards() {
console.log('timecards', this.timecards);
//code dependent on timecard data
}
async onSubmit() {
await this.getTimecard();
this.checkCards();
}
Why Isn't checkOverlap waiting for getTimecard to return its data?
回答1:
To make a function awaitable the function needs to return a Promise.
loadTimecards() {
return new Promise(resolve => {
this.sending = true;
this._service.getTimecard().subscribe(data => {
this.sending = false;
this.timecards = data;
resolve();
});
});
}
来源:https://stackoverflow.com/questions/51860844/angular-5-await-not-waiting