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.

How to properly reject Promise and why deferring is anti-pattern?

See original GitHub issue

Hi @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:closed
  • Created 9 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
benjamingrcommented, Feb 27, 2015

Hi @fourpixels

Doing throw new Error is the correct approach (although please consider throwing custom errors and not just Error). Doing return Promise.reject(...) will also work

The 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:

myApiDbIncremenet().catch(function(){});

Which is similar to a synchronous:

try {
     doSomething();
} catch(e){
    // suppress exception
}

Also - for your findFirst example it could simply be:

function findFirst() {
    return Entity.find(1).then(function(result) {
           if(!result) throw new Error("No results found"); // consider a custom error
           else return result;
    });
}
0reactions
petkaantonovcommented, Feb 27, 2015

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:

function incrementUserCounter() {
     return query(...).catch(Promise.OperationalError, function(ignore){});
}
Read more comments on GitHub >

github_iconTop 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 >

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