Noitidart Noitidart - 1 year ago 111
Javascript Question

Promise.defer standard?

I was working with Promises and prefer to use it like this:

function Deferred() {
this.resolve = null;
this.reject = null;
this.promise = new Promise(function(resolve, reject) {
this.resolve = resolve;
this.reject = reject;

function somethingAsync() {

var deferred = new Deferred();

// do stuff then deferred.resolve();

return deferred.promise;

I just came across though in Firefox
which gives me the same thing, is this standard? Or just specific to Firefox? I can't find it in the Promise docs of Firefox even -

Answer Source

Promise.defer was a suggestion at one point but it was decided to not include it in the spec but to instead include the promise constructor which uses the revealing constructor pattern.

It was implemented in Firefox and Chrome and later removed from Chrome. It is not a standard but was a proposal at one point.

Your usage of the promise constructor was explicitly supported as a use case when it was designed.

The reason the committee decided to go with the promise constructor was because it guards against synchronous throws by default:

new Promise((resolve, reject) => {

Had the promise constructor not done this - you would have to potentially .catch and } catch(e) { on every promise returning function invocation which can be frustrating. The promise constructor establishes an invariant where .catch is sufficient.

I'd also like to point out that outside of converting callback APIs - I can count the number of times I've used the promise constructor on one hand. Typically your code should have close to zero deferreds or usages of the promise constructor.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download