如何在两个 javascript 函数之间实现一个承诺?

how to implement a promise between two javascript functions?

我的 angular 应用程序中有两个函数,在第一个函数中我将加载程序设置为 true,我显示和隐藏了我的 html 的一些组件,最后我调用另一个函数来做一个数据的记录。

  handleClick() {

    if (this.isDisabledSaveButton) { 
      this.setData();
    } else {
      this.emitLoader = true;
      this.showForm();
      this.hideTools();
      this.formSubmit();
    }
  }

在函数 this.formSubmit() 的末尾,在订阅和不同的条件之后,我将加载器设置为 false,我想通过 Promise[=14= 将它传递给第一个函数]

formSubmit() {
     this.loadFooter = false;
     if (this.formSubscription !== undefined) {
      this.formSubscription.unsubscribe();
    }

    // differents conditions....

        

    this.emitLoader = false;
    this.blockButtons();
}

是我尝试实现的第一个承诺,对于许多示例,我看到我无法 return 第一个函数的值以便在成功订阅后进行重定向

handleClick() {

    if (this.isDisabledSaveButton) { 
      this.setData();
    } else {
      this.emitLoader = true;
      this.showForm();
      this.hideTools();
      this.formSubmit();
      let promise = new Promise((resolve, reject) => {
        this.router.navigate(['pages/incineris/conventions']);
      });
      return promise;
    }
  }

我应该如何将这两个函数与 promise 联系起来? 我想在 formSubmit() 函数中解决 this.emitLoader = false 之后的承诺。 谢谢大家的帮助

formSubmit() 正在执行异步操作。这代表了对未来结果的“承诺”。我在这里创建了一个新的 Promise 对象,但通常您的网络请求本身可能会在这里返回,因为该库可能会返回它自己的承诺。

formSubmit() {
  // ...
  return new Promise((resolve, reject) {
    // ...
  });

handleClick() 函数等待 promise 完成,并使用 .then() 继续执行成功。

handleClick() {
  // ...
  this.formSubmit().then(() => {
    this.router.navigate(['pages/incineris/conventions']);
  });
}