Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle the if-else in promise then?

In some case, when I get a return value from a promise object, I need to start two different then() precesses depend on the value's condition, like:

promise().then(function(value){     if(//true) {         // do something     } else {         // do something      } }) 

I'm thinking maybe I can write it like:

promise().then(function(value){     if(//true) {         // call a new function which will return a new promise object         ifTruePromise().then();     } else {         ifFalsePromise().then();     } }) 

but with this, I have two questions:

  1. I'm not sure if it's a good idea to start a new promise-then process in a promise;

  2. what if I need the two process to call one function in the last? It means they have the same "terminal"

I tried to return the new promise to keep the original chain like:

promise().then(function(value){     if(//true) {         // call a new function which will return a new promise object         // and return it         return ifTruePromise();     } else {         // do something, no new promise         // hope to stop the then chain     } }).then(// I can handle the result of ifTruePromise here now); 

but in this case, whether it's true or false, the next then will work.

SO, what's the best practice to handle it?

like image 626
Brick Yang Avatar asked Oct 21 '15 10:10

Brick Yang


People also ask

How do you resolve a promise then?

Promise resolve() method: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.

Does a promise need a then?

You can think of a promise as a placeholder for a value that hasn't been computed yet. However, there's no way to get a promise's value from the promise directly - you need to call the then() function to register a callback that JavaScript will call when the value is computed.

How do you wait for promise then?

The keyword await is used to wait for a Promise. It can only be used inside an async function. This keyword makes JavaScript wait until that promise settles and returns its result.

What does then do promise?

The then() method returns a Promise. It takes two arguments: callback functions for the success and failure cases of the Promise. The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future.


1 Answers

As long as your functions return a promise, you can use the first method that you suggest.

The fiddle below shows how you can take different chaining paths depending on what the first resolved value will be.

function myPromiseFunction() {  	//Change the resolved value to take a different path      return Promise.resolve(true);  }    function conditionalChaining(value) {      if (value) {          //do something          return doSomething().then(doSomethingMore).then(doEvenSomethingMore);      } else {          //do something else          return doSomeOtherThing().then(doSomethingMore).then(doEvenSomethingMore);      }  }    function doSomething() {      console.log("Inside doSomething function");      return Promise.resolve("This message comes from doSomeThing function");  }    function doSomeOtherThing() {      console.log("Inside doSomeOtherthing function");      return Promise.resolve("This message comes from doSomeOtherThing function");  }    function doSomethingMore(message) {      console.log(message);      return Promise.resolve("Leaving doSomethingMore");  }    function doEvenSomethingMore(message) {      console.log("Inside doEvenSomethingMore function");      return Promise.resolve();  }    myPromiseFunction().then(conditionalChaining).then(function () {      console.log("All done!");  }).  catch (function (e) {    });

You can also just make one conditional chaining, assign the return promise to a variable and then keep executing the functions that should be run either way.

function conditionalChaining(value){     if (value) {         //do something         return doSomething();     } else{         //do something else         return doSomeOtherThing();     } }  var promise = myPromiseFunction().then(conditionalChaining);  promise.then(function(value){     //keep executing functions that should be called either way }); 
like image 96
Daniel B Avatar answered Sep 28 '22 06:09

Daniel B