javascript - 如何在 ionic 4 中使用 alertcontroller 返回 promise ?

标签 javascript typescript ionic-framework promise ionic4

我正在尝试将此 ionic 3 代码转换为 ionic 4,但我不知道 promise 如何在 ionic 4 上工作。我尝试查看文档,但找不到任何 promise 的解决方案

async generateAlert(header, message, ok, notOk): Promise<boolean> {
    return new Promise((resolve, reject) => {
        let alert = await this.alertController.create({
            header: header,
            message: message,
            buttons: [
                {
                    text: notOk,
                    handler: _=> reject(false)
                },
                {
                    text: ok,
                    handler: _=> resolve(true)
                }
            ]
        })
        await alert.present();
    });
}

最佳答案

如你所见 here :

The await operator is used to wait for a Promise.

所以 await 只是另一种使用 promises 的方式,就像你在下面的例子中看到的那样:

// Method that returns a promise
private resolveAfter2Seconds(x: number): Promise<number> { 
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}

// Using await
private async f1(): Promise<void> {
  var x = await resolveAfter2Seconds(10);
  console.log(x); // 10
}

// Not using await
private f2(): void {
  resolveAfter2Seconds(10).then(x => {
    console.log(x); // 10
  });
}

f1(){...} 中,您可以看到应用如何在执行下一行代码之前等待 promise 得到解决。这就是为什么你可以做类似的事情

var x = await resolveAfter2Seconds(10);
console.log(x); // 10

没有将 console.log(x) 放在 .then(() => {...}) block 中。

f2() 中,因为我们不使用 await,所以应用程序不会在执行下一行代码之前等待 promise 被解析,所以我们必须使用 then 在控制台打印结果:

resolveAfter2Seconds(10).then(x => {
  console.log(x); // 10
});

也就是说,如果您只想创建一个方法,当用户选择 ok/notOk 按钮时显示警报并返回 true/false,您可以执行以下操作(根本不使用 await):

private generateAlert(header: string, message: string, ok: string, notOk: string): Promise<boolean> {
  return new Promise((resolve) => {
    // alertController.create(...) returns a promise!
    this.alertController
      .create({
        header: header,
        message: message,
        buttons: [
          {
            text: notOk,
            handler: () => resolve(false);
          },
          {
            text: ok,
            handler: () => resolve(true);
          }
        ]
      })
      .then(alert => {
        // Now we just need to present the alert
        alert.present();
      });
  });
}

然后你可以像这样使用那个方法

private doSomething(): void {

  // ...

  this.generateAlert('Hello', 'Lorem ipsum', 'Ok', 'Cancel').then(selectedOk => {
    console.log(selectedOk);
  });

}

关于javascript - 如何在 ionic 4 中使用 alertcontroller 返回 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56637790/

相关文章:

javascript - 是否选中 Html.CheckBox

javascript - setState 不更新状态

javascript - 用作 onclick 处理程序的函数运算符和函数声明的行为不同

typescript - Angular2 有没有办法动态设置 bootstrap col?

typescript - 如何在 Ionic 2 选项卡中触发自定义功能?

javascript - 可以运行 Javascript 但不能运行 jQuery?

Angular 4, typescript 将接口(interface)属性分配给变量

angular - 如何使用 angular 2+ 创建和下载具有动态内容的文本/Json 文件

angular - 如何将 Google map 与 Ionic 3 结合使用

javascript - 有没有办法使用 ng-repeat 动态更改对象的键