Skip to content

Accessing resolve/reject and promise at the same time #23

@domenic

Description

@domenic

While implementing #18 as it stands, @wycats has run into the issue @erights outlined, of wanting access to both resolve/reject and promise at the same time. This is pretty common, so let's dig into it a bit more:

Solution 1: Awkwardness

var resolve;
var promise = new Promise((resolve_) => { resolve = resolve_; });
// use both `resolve` and `promise`.

Note that this only works if the resolver function is called synchronously (see #20).

Solution 2: Asyncness

var promise = new Promise((resolve) => {
  // use both `resolve` and `promise`
});

If the resolver function is called asynchronously (again, see #20), you can use both at once.

Solution 3: this

var promise = new Promise(function (resolve) {
  // use both `resolve` and `this`,
  // where `this` becomes `promise` after the constructor returns
});

@wycats's solution was to bind the promise-to-be-returned as this, i.e. as an implicit zeroth argument inside the resolver function. I don't like this pattern in general, and think it's a bit unfortunate that such a pattern would require not using ES6 arrow functions. But some people like it, including one of our favorite implementers, so, meh?

Solution 4: extra param

var promise = new Promise(function (resolve, reject, promise) {
  // use both `resolve` and `promise`
});

A variation on solution 3, this makes the parameter explicit instead of implicit, as suggested by @briancavalier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions