0

I want the saveAsAndGo() function to be performed before the callback() but thats not the case , how can I make sure that it is performed before the callback ?

async saveAndGoTo(): Promise<void> {
  Store.setFileChanged(false);
  this.fileName = Store.getSelectedFile().name;
  if (this.fileName === 'New') {
    this.fileNotSavedDialog.hideDialog();
    await this.saveAsAndGoTo().then(() => {
      this.callback();
    });
  } else {
    this.fileNotSavedDialog.hideDialog();
    await this.saveDialog.updateFile().then(() => {
      this.callback();
    });
  }
}
async saveAsAndGoTo(): Promise<void> {
  await this.saveDialog.showDialog();
}
ofhouse
  • 3,047
  • 1
  • 36
  • 42
salma
  • 13
  • 5
  • Check this out: https://stackoverflow.com/questions/55019621/using-async-await-and-then-together – mr.rusik Apr 14 '22 at 13:30
  • @mr.rusik I am not trying to catch an error , I just want to perform task before the other – salma Apr 14 '22 at 13:41
  • Please provide a [mre] that clearly demonstrates the issue you are facing. Ideally someone could paste the code into a standalone IDE like [The TypeScript Playground (link here!)](https://tsplay.dev/mMMXlm) and immediately get to work solving the problem without first needing to re-create it. So there should be no pseudocode, typos, unrelated errors, or undeclared types or values. – jcalz Apr 14 '22 at 14:00
  • Why would you even want to use a callback when the caller can simply wait for the returned promise? – Bergi Apr 14 '22 at 14:05
  • "*I want the `saveAsAndGo()` function to be performed before the `callback()` but thats not the case*" - it certainly is. Your code does call `this.saveAsAndGoTo()` first, and `this.callback()` after it; it cannot happen the other way round. If your actual issue is that the callback doesn't *wait* until `saveAsAndGoTo` is finished, that would be a problem in the `this.saveDialog.showDialog()` call whose implementation you haven't shown us. – Bergi Apr 14 '22 at 14:09

0 Answers0