I have a function that runs some arbitrary code, called calculate(). I have an if condition and if it is true I present an ionic confirm alert.
I can get the confirm alert to popup, however, I am trying to use async/await to wait for a response in the confirmation, but my understanding of async/await must be wrong. Here is basically what I am doing:
import { AlertController } from '@ionic/angular';
export class Calculator {
private cancelResults:boolean = false;
constructor(private alertController:AlertController) {}
async calculate() {
// If my condition is true.
if (true) {
// show the user a confirm alert.
await this.warn();
// break out of function since they hit cancel.
if (this.cancelResults) return;
}
// The user hit Okay, continue with this function.
}
async warn() {
const alert = await this.alertController.create({
header: 'confirm',
message: 'my message',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: (blah) => {
console.log('they hit cancel');
this.cancelResults = true;
return new Promise(resolve => setTimeout(resolve, 2000));
}
}, {
text: 'Okay',
handler: () => {
console.log('they hit ok');
return new Promise(resolve => setTimeout(resolve, 2000));
}
}
]
});
await alert.present();
}
}
When the confirmation pops up, the remainder of the calculate() fn continues. I want it to wait for the confirmation response.
Any ideas of how to achieve this?