question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Early Returns out of a long promise chain

See original GitHub issue

DoSomethingAsync()
.then(step1)
.then(step2)
.then(step3)
.then(step4)
.then(step5)
function step2() {
  if (someCondition) {
    // I want to early return and skip the rest of the promise chain
  }

}

Is there an elegant way to do early return for a long promise chain?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:72
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

22reactions
shanemilehamcommented, Jul 13, 2016

I think it would be a great feature to have a native break and early return - the existing solutions are exceedingly clunky. Can this be opened as a feature request?

9reactions
spioncommented, Jul 13, 2016

For exceptions (unexpected conditions), you can already throw to terminate the chain early. For most other cases, nesting one level works fine. If you have something that doesn’t fit the above two solutions feel free to discuss, but given that async/await is just around the corner, it may not be worth the effort (IMO)

Read more comments on GitHub >

github_iconTop Results From Across the Web

ExpressJS / NodeJS / Promises: Return early from ...
My question is, if I find a game early in the promise chain (i.e. there's either a user's existing game or another user...
Read more >
Promises chaining
When a handler returns a value, it becomes the result of that promise, so the next .then is called with it. A classic...
Read more >
How to Break Out of a Promise Chain with JavaScript - YouTube
Ever wonder how to break out of a promise chain with JavaScript? If yes, than you are in the right place. I cover...
Read more >
Using promises - JavaScript - MDN Web Docs
With promises, we accomplish this by creating a promise chain. ... last handler is not returned, the next then handler will be called...
Read more >
Error handling in long Promise chains | by Arthur Xavier
It turns out every step of the form, that is, every API call on submit could return a different error, which should be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found