在另一个函数完成后调用该函数

发布于 2025-01-19 18:03:32 字数 1219 浏览 1 评论 0原文

我正在尝试使用承诺来确保一个功能“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文