admin管理员组

文章数量:1128698

I was writing code that does something that looks like:

function getStuffDone(param) {
    return new Promise(function(resolve, reject) {
        myPromiseFn(param+1)  
        .then(function(val) { 
            resolve(val);     
        }).catch(function(err) {
            reject(err);
        });
    });
}

Or:

function getStuffDone(param) {
    var d = Q.defer(); /* or $q.defer */
    // or = new $.Deferred() etc.
    myPromiseFn(param+1)
    .then(function(val) { /* or .done */
        d.resolve(val);
    }).catch(function(err) { /* .fail */
        d.reject(err);
    });
    return d.promise; /* or promise() */
}

I was writing code that does something that looks like:

function getStuffDone(param) {
    return new Promise(function(resolve, reject) {
        myPromiseFn(param+1)  
        .then(function(val) { 
            resolve(val);     
        }).catch(function(err) {
            reject(err);
        });
    });
}

Or:

function getStuffDone(param) {
    var d = Q.defer(); /* or $q.defer */
    // or = new $.Deferred() etc.
    myPromiseFn(param+1)
    .then(function(val) { /* or .done */
        d.resolve(val);
    }).catch(function(err) { /* .fail */
        d.reject(err);
    });
    return d.promise; /* or promise() */
}

Someone told me this is called the "deferred antipattern" or the "Promise constructor antipattern" respectively, what's bad about this code and why is this called an antipattern?

Share Improve this question edited Mar 21, 2024 at 8:30 dumbass 27.2k4 gold badges36 silver badges73 bronze badges asked May 22, 2014 at 10:07 Benjamin GruenbaumBenjamin Gruenbaum 276k89 gold badges518 silver badges513 bronze badges 7
  • 1 or is having the catch block in the getStuffDone wrapper the antipattern? – The Dembinski Commented Jan 6, 2017 at 23:45
  • 3 At least for the native Promise example you also have unnecessary function wrappers for the .then and .catch handlers (i.e. it could just be .then(resolve).catch(reject).) A perfect storm of anti-patterns. – Noah Freitas Commented Aug 30, 2017 at 17:48
  • 11 @NoahFreitas that code is written that way for didactic purposes. I wrote this question and answer in order to help people who run into this issue after reading a lot of code looking like that :) – Benjamin Gruenbaum Commented Aug 30, 2017 at 18:28
  • 8 What's up the odd side-by-side code examples? Never seen that here before, find it quite confusing. Had to check the revision history to understand that both are example of the same thing. – Alex Commented Aug 5, 2021 at 10:11
  • 1 @Rainb more like this question isn't intended for people who likely don't yet know what monads are or that they chain. I'm sure that group included you at some point (as well as me) :) – Benjamin Gruenbaum Commented Jan 31, 2022 at 9:54
 |  Show 2 more comments

3 Answers 3

Reset to default 457

The deferred antipattern (now explicit-construction anti-pattern) coined by Esailija is a common anti-pattern people who are new to promises make, I've made it myself when I first used promises. The problem with the above code is that is fails to utilize the fact that promises chain.

Promises can chain with .then and you can return promises directly. Your code in getStuffDone can be rewritten as:

function getStuffDone(param){
    return myPromiseFn(param+1); // much nicer, right?
}

Promises are all about making asynchronous code more readable and behave like synchronous code without hiding that fact. Promises represent an abstraction over a value of one time operation, they abstract the notion of a statement or expression in a programming language.

You should only use deferred objects when you are converting an API to promises and can't do it automatically, or when you're writing aggregation functions that are easier expressed this way.

Quoting Esailija:

This is the most common anti-pattern. It is easy to fall into this when you don't really understand promises and think of them as glorified event emitters or callback utility. Let's recap: promises are about making asynchronous code retain most of the lost properties of synchronous code such as flat indentation and one exception channel.

What's wrong with it?

But the pattern works!

Lucky you. Unfortunately, it probably doesn't, as you likely forgot some edge case. In more than half of the occurrences I've seen, the author has forgotten to take care of the error handler:

function bad() {
    return new Promise(function(resolve) {
        getOtherPromise().then(function(result) {
            resolve(result.property.example);
        });
    })
}

If the other promise is rejected, this will happen unnoticed instead of being propagated to the new promise (where it would get handled) - and the new promise stays forever pending, which can induce leaks.

The same thing happens in the case that your callback code causes an error - e.g. when result doesn't have a property and an exception is thrown. That would go unhandled and leave the new promise unresolved.

In contrast, using .then() does automatically take care of both these scenarios, and rejects the new promise when an error happens:

function good() {
    return getOtherPromise().then(function(result) {
        return result.property.example;
    })
}

The deferred antipattern is not only cumbersome, but also error-prone. Using .then() for chaining is much safer.

But I've handled everything!

Really? Good. However, this will be pretty detailed and copious, especially if you use a promise library that supports other features like cancellation or message passing. Or maybe it will in the future, or you want to swap your library against a better one? You won't want to rewrite your code for that.

The libraries' methods (then) do not only natively support all the features, they also might have certain optimisations in place. Using them will likely make your code faster, or at least allow to be optimised by future revisions of the library.

How do I avoid it?

So whenever you find yourself manually creating a Promise or Deferred and already existing promises are involved, check the library API first. The Deferred antipattern is often applied by people who see promises [only] as an observer pattern - but promises are more than callbacks: they are supposed to be composable. Every decent library has lots of easy-to-use functions for the composition of promises in every thinkable manner, taking care of all the low-level stuff you don't want to deal with.

If you have found a need to compose some promises in a new way that is not supported by an existing helper function, writing your own function with unavoidable Deferreds should be your last option. Consider switching to a more featureful library, and/or file a bug against your current library. Its maintainer should be able to derive the composition from existing functions, implement a new helper function for you and/or help to identify the edge cases that need to be handled.

Now 7 years later there is a simpler answer to this question:

How do I avoid the explicit constructor antipattern?

Use async functions, then await every Promise!

Instead of manually constructing nested Promise chains such as this one:

function promised() {
  return new Promise(function(resolve) {
    getOtherPromise().then(function(result) {
      getAnotherPromise(result).then(function(result2) {
        resolve(result2);
      });
    });
  });
}

just turn your function async and use the await keyword to stop execution of the function until the Promise resolves:

async function promised() {
   const result =  await getOtherPromise();
   const result2 = await getAnotherPromise(result);
   return result2;
}

This has various benefits:

  • Calling the async function always returns a Promise, which resolves with the returned value and rejects if an error get's thrown inside the async function
  • If an awaited Promise rejects, the error get's thrown inside the async function, so you can just try { ... } catch(error) { ... } it like the synchronous errors.
  • You can await inside loops and if branches, making most of the Promise chain logic trivial
  • Although async functions behave mostly like chains of Promises, they are way easier to read (and easier to reason about)

How can I await a callback?

If the callback only calls back once, and the API you are calling does not provide a Promise already (most of them do!) this is the only reason to use a Promise constructor:

 // Create a wrapper around the "old" function taking a callback, passing the 'resolve' function as callback
 const delay = time => new Promise((resolve, reject) =>
   setTimeout(resolve, time)
 ); 

 await delay(1000);

If await stops execution, does calling an async function return the result directly?

No. If you call an async function, a Promise gets always returned. You can then await that Promise too inside an async function. You cannot wait for the result inside of a synchronous function (you would have to call .then and attach a callback).

Conceptually, synchronous functions always run to completion in one job, while async functions run synchronously till they reach an await, then they continue in another job.

本文标签: javascriptWhat is the explicit promise construction antipattern and how do I avoid itStack Overflow