0

I'm creating a function with returns a Promise, but may have some promise in cascade:

  mainFunction(): Promise<boolean> {

     return this.function1().then(
      (data) => {

        if(data.condition1()){
          this.function2()
            .then(() => {
              return true // here my problem
            })
        } else if (data.condition2()){
          return true;
        } else {
          return false;
        }
      }
    );
  }

  function1: Promise<string>{
    //return promise<string>
  } 

  function2: Promise<any>{
    //return promise<any>
  }

mainFunction works fine in "else if" and "else", where no Promise are in cascade but in "if" where I ask for promise after another promise true is not returned.

How can I do this?

cucuru
  • 3,456
  • 8
  • 40
  • 74
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) - You need to return the promise from your first `if` branch, otherwise the function will just finish and return null. – Joe Clay Jan 30 '18 at 11:58

2 Answers2

2

you can do something like below

mainFunction(): Promise<boolean> {
return new Promise((resolve, reject) => {
  this.function1().then((data) => {
    if (data.condition1()) {
      this.function2()
        .then(() => {
          resolve(true);
        })
    } else if (data.condition2()) {
      resolve(true);
    } else {
      reject(false);
    }
  })
})
Imdad Ali
  • 727
  • 1
  • 8
  • 18
  • Why are you creating another promise if you have the promise to return? It works but it is needed, just return the function2 – Alan Grosz Jan 30 '18 at 12:37
  • no, it's not returning function2. it returns promise in every situation. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – Imdad Ali Jan 30 '18 at 12:41
  • yes, it is doing what I and you said. But I said that isn't needed because you have already a promise in function2, so you only have to return it to make a chain promise (look my response below). You are adding an overhead, very little but is a bad practise – Alan Grosz Jan 30 '18 at 12:49
  • but else if section returns boolean, I wrapped the whole thing in a promise to cover that section as well. – Imdad Ali Jan 30 '18 at 12:53
  • Is the same, in the else you are resolving o rejecting a boolean too. I repeat, It works but you are adding another promise. Is like adding an empty
    above another
    – Alan Grosz Jan 30 '18 at 12:58
  • there is a difference between returning 'true' and returning 'resolve(true)', in the prior case boolean is return while in the late a promise is returned which the requirements of the main function, which require a Promise to be returned. Please have a look at the mdn link. – Imdad Ali Jan 30 '18 at 13:10
  • You are not returning resolve(true) you are returning a promise that inside is resolving true or false – Alan Grosz Jan 30 '18 at 13:20
1

Remove the then in the function2 call and return it to make the cascade:

mainFunction(): Promise<boolean> {

 return this.function1().then(
  (data) => {

    if(data.condition1()){
      return this.function2();
    } else if (data.condition2()){
      return true;
    } else {
      return false;
    }
  }
);

}

Alan Grosz
  • 1,175
  • 10
  • 15