How to properly reject Promise and why deferring is anti-pattern?
See original GitHub issueHi @petkaantonov , I would love if you have spare 10 minutes to read this question of mine: http://stackoverflow.com/q/28761365/2065080
I just don’t understand why deferring is an anti-pattern (as well as .then(success, fail)). I’ve posted an example (which I can also post here) with my real-life problem that I can’t seem to solve properly. I can’t get promisify
to work and I ended up deferring.
You’ve stated those anti-patterns, but I can’t seem to understand why is that so.
Thanks in advance!
Issue Analytics
- State:
- Created 9 years ago
- Comments:16 (7 by maintainers)
Top Results From Across the Web
Promises and the Deferred Antipattern - Revue
Only the executor function can resolve or reject the promise, unless you explicitly pull `resolve()` and `reject()` out of the executor ...
Read more >What is the explicit promise construction antipattern and how ...
The Deferred antipattern is often applied by people who see promises [only] as an observer pattern - but promises are more than callbacks:...
Read more >Anti-patterns - Bluebird JS
In the explicit construction anti-pattern, promise objects are created for no reason, complicating code. First example is creating deferred object when you ...
Read more >ES6 Promise Anti-Patterns and Best Practices
ES6 Promises are much different than using callbacks. This article covers various anti-patterns and best practices for using Promises in ...
Read more >Common Promise Anti-Patterns and How to Avoid Them
Engineers making the move from callbacks to promises make common mistakes and introduce anti-patterns into their codebase. During my time working with ...
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
Hi @fourpixels
Doing
throw new Error
is the correct approach (although please consider throwing custom errors and not justError
). Doingreturn Promise.reject(...)
will also workThe fact it “blows up” if no handler is attached is great. It’s the same thing that happens in synchronous exceptions - if your application has an error and it was not handled you’d expect it to at least tell you.
If you want to suppress an exception it’s quite easy to do so:
Which is similar to a synchronous:
Also - for your findFirst example it could simply be:
If you don’t care about an operation like increasing the user counter failing, then handling the error is simply using an empty catch handler matching
Promise.OperationalError
. Then everyone reading the code understands that the intent is to ignore any possible error.You don’t need to write it anywhere but in the
incrementUserCounter()
function: