Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promises and flow control: Early exit [duplicate]

I recently started writing promise code in coffeescript/javascript and I love it. The one thing that I don't understand is how to handle flow control with promises. Consider the following code with Q.

outerFunction = ->
  Q()
    .then ->
      doSomethingAsync
    .then (result) ->
      return result if result is someSpecialValue 
      #do I really have to continue this chain or nest my promises in a new promise chain?
      ...
    .then ->
      ...
    .then ->
      ...

I want to return to the caller early. Is this even possible?

I don't want to use magic exceptions for flow control. Do I need to do something like this or is there a better way?

...
.then (result) ->
  return result if result is someSpecialValue
  return Q()
    .then -> 
      ...
    .then -> 
      ...
like image 514
CharlesTWall3 Avatar asked Oct 26 '25 03:10

CharlesTWall3


1 Answers

Here's a more complete answer, returning to the caller isn't possible. Because Promise are running asynchrously... in other words, the caller already returned when the promise started working. So returning to the caller isn't possible because the caller is already gone.

If you want to leave the promise, you can simply call this.reject() You can reject with a parameter. It will get caught in a catch promise. You can reject from a catch clause too if you don't want it to process any more then. Then at some point, this will result in the promise failing.

It might not do exactly what you want because you'll have to handle at least on final catch to handle the errors or why you left early from the promise. But even the catch is a promise.

outerFunction = ->
  Q()
    .then ->
      doSomethingAsync
    .then (result) ->

      if error
         this.reject(result)

      return result if result is someSpecialValue 

    .then ->
      ...
    .then ->
      ...
    .catch (error) ->
      console.log("Handling error", error)

Here's a bit more documentation on promises: http://promisejs.org/ This is a good read.

I hope you understand that using reject is pretty much like trying to early exit from a function couple of stacked function by raising an exception. I do not encourage this as it could be quite terrible and hard to understand the logic behind it. If you have to early exit from a promise even though there is no exceptions or error, then you probably have to rethink about your flow.

What you could do is this instead:

outerFunction = ->
  Q()
    .then ->
      doSomethingAsync
    .then (result) ->

      if return_now
         return result if result is someSpecialValue
      return Q()
         .then ->
           ...
         .then ->
           ...
         .then ->
           ...

You could return your result or return a promise that will continue the process chain.

like image 63
Loïc Faure-Lacroix Avatar answered Oct 28 '25 16:10

Loïc Faure-Lacroix



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!