在另一个函数完成后调用该函数
我正在尝试使用承诺来确保一个功能“ cumber()”在另一个函数之后“ save()”已完成。我很久没有使用诺言了,我对语法感到困惑。我还尝试使用异步并等待,但无济于事。
public globalFunction() {
//call save() first, wait until fully completed and then call submit()
this.save().then(() => {
this.submit(approver);
})
//this.save().then(res => this.submit(approver));
}
save() {
return new Promise<void>((resolve, reject) => {
this.service.save(this.item.id, updatedItemList).pipe(
first(),
catchError((err) => {
this.toastService.show(`Save failed`, { type: "error" });
return null;
})
).subscribe((success: boolean) => {
this.loadItemss().then(() => {
this.uploadDocs();
});
this.toastService.show("Successfully saved", { type: "success" });
});
resolve();
});
}
private submit(approver: Lookup): void {
this.service.submit(this.item.id, approver).pipe(
first(),
catchError((error) => {
this.toastService.show(`Submit failed`, { type: 'error' });
return null;
})
).subscribe((success: boolean) => {
this.toastService.show(`Successfully submitted`, { type: 'success' })
});
}
I am trying to use a promise to ensure one function, "submit()" is called after the another function, "save()" has been completed. I haven't used promises in a long while and I'm confused on the syntax. I also tried using async and await but to no avail.
public globalFunction() {
//call save() first, wait until fully completed and then call submit()
this.save().then(() => {
this.submit(approver);
})
//this.save().then(res => this.submit(approver));
}
save() {
return new Promise<void>((resolve, reject) => {
this.service.save(this.item.id, updatedItemList).pipe(
first(),
catchError((err) => {
this.toastService.show(`Save failed`, { type: "error" });
return null;
})
).subscribe((success: boolean) => {
this.loadItemss().then(() => {
this.uploadDocs();
});
this.toastService.show("Successfully saved", { type: "success" });
});
resolve();
});
}
private submit(approver: Lookup): void {
this.service.submit(this.item.id, approver).pipe(
first(),
catchError((error) => {
this.toastService.show(`Submit failed`, { type: 'error' });
return null;
})
).subscribe((success: boolean) => {
this.toastService.show(`Successfully submitted`, { type: 'success' })
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论