lundi 17 juin 2019

How to use "Promise.then =>" outside of a conditional in Typescript

I'm writing a function in TypeScript that looks like this:

record_time():any {
    if (some_condition) {
      axios.post('api/time').then(() =>
        updateTimeDisplay()
      );
    }else {
      axios.put('api/time').then(() =>
        updateTimeDisplay()
      )
    }
  }

And it works, but as you can see, I'm calling the same line, updateTimeDisplay(), regardless of the conditional. For conciseness, I'd like to know if there is a way to rewrite this so that I'd only write updateTimeDisplay() once. Tried something like below, but the code broke with this error: Cannot find name 'then'.Vetur(2304)

record_time():any {
    if (some_condition) {
      axios.post('api/time');
    }else {
      axios.put('api/time');
    }.then(() =>
      updateTimeDisplay()
    )
  }

Aucun commentaire:

Enregistrer un commentaire