Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sequence the running of two functions that both return promises?

My code does a check of word.statusId to see if it's dirty. If it is then it does an update of word and then if that works it updates wordForms. If it's clean then it just updates wordForms. Can someone advise me if this is the correct way to handle running one promise after another?

update = (): ng.IPromise<any> => {
        var self = this;
        if (self.word.statusId != 3) {
            return self.wordEditSubmit()
                .then(() => {
                    return self.wordFormCheckAndUpdate();
                })
        } else {
            return self.wordFormCheckAndUpdate();
        }
    }
like image 301
Samantha J T Star Avatar asked Nov 27 '25 07:11

Samantha J T Star


1 Answers

What you have described as the desired behaviour is indeed the actual behaviour.

As you are using arrow functions you do not need to store the value of this:

update = (): ng.IPromise<any> => {
  if (this.word.statusId != 3) {
    return this.wordEditSubmit()
      .then(() => this.wordFormCheckAndUpdate())
  } else {
    return this.wordFormCheckAndUpdate();
  }
}

Simplified again using a ternary condition:

update = (): ng.IPromise<any> => {
  return this.word.statusId != 3
    ? this.wordEditSubmit().then(() => this.wordFormCheckAndUpdate())
    : this.wordFormCheckAndUpdate();
}
like image 56
sdgluck Avatar answered Nov 29 '25 21:11

sdgluck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!