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:
- Created 8 years ago
- Reactions:72
- Comments:14 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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?
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)