admin管理员组文章数量:1323150
I am trying to convert this ionic 3 code into ionic 4 but I don't know how the promise works on ionic 4. I tried looking into the documentations and I can't find any solutions to promises
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();
});
}
I am trying to convert this ionic 3 code into ionic 4 but I don't know how the promise works on ionic 4. I tried looking into the documentations and I can't find any solutions to promises
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();
});
}
Share
asked Jun 17, 2019 at 19:32
KevinKevin
833 silver badges7 bronze badges
1
- Please, can you tell what you want to do ? – Kishan Bharda Commented Jun 18, 2019 at 4:31
1 Answer
Reset to default 9As you can see here:
The await operator is used to wait for a Promise.
So await
is just another way to work with promises like you can see in the following example:
// 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
});
}
In f1(){...}
you can see how the app will wait for the promise to be resolved before executing the next line of code. That's why you can do something like
var x = await resolveAfter2Seconds(10);
console.log(x); // 10
without putting that console.log(x)
in a .then(() => {...})
block.
In f2()
since we don't use await
, the app won't wait for the promise to be resolved before executing the next line of code, so we must use then
to print the result in the console:
resolveAfter2Seconds(10).then(x => {
console.log(x); // 10
});
That being said, if you just want to create a method that shows an alert and returns true/false when the user selects the ok
/notOk
buttons, you can do the following (which doesn't use await
at all):
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();
});
});
}
Then you can use that method like this
private doSomething(): void {
// ...
this.generateAlert('Hello', 'Lorem ipsum', 'Ok', 'Cancel').then(selectedOk => {
console.log(selectedOk);
});
}
本文标签: javascriptHow to return a promise with alertcontroller in ionic 4Stack Overflow
版权声明:本文标题:javascript - How to return a promise with alertcontroller in ionic 4? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742142994a2422665.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论