John Morris
John Morris

Reputation: 721

Javascript: Access Promise object from within function passed to Promise constructor

I am trying to create a new javascript Promise, and in function I pass to the new Promise(func) I would like to have access to the promise object I created to store and use later to resolve when something has occurred, rather than call functions to do the work from within the promise function.

I have tried:

let promise = new Promise((resolve, reject) => {
    this._setupPromise = { promise, resolve, reject };
});

But of course the "promise" variable is not yet set, so this._setupPromise.promise is undefined.

I can change it like this to add the promise to the object outside of the new Promise, but it just doesn't feel right since it could run before the Promise constructor function:

this._setupPromise = {};
let promise = new Promise((resolve, reject) => {
    this._setupPromise.resolve = resolve;
    this._setupPromise.reject = reject;
});
this._setupPromise.promise = promise

Is there any good solution to my delimma?

For anyone wondering why I would resolve/reject the Promise outside of calling it from within the Promise constructor function, I am optionally promisifying some legacy code, and to avoid a massive refactor of some existing use cases, it helps me resolve the promises elsewhere optionally.

Upvotes: 1

Views: 657

Answers (2)

jfriend00
jfriend00

Reputation: 707148

Making my comment into an answer (and adding more explanation) since it actually was the answer you were looking for:

Your second code block is the way to do it. The promise variable itself does not have a value until the executor has returned and then the constructor has returned so you can't assign it to something else until after the executor and the promise constructor are done.

Since you don't have any asynchronous code inside the executor function, things will execute sequentially so this doesn't look like it would be a problem.

You may find that using a Deferred object (which just encapsulates the saving of the resolve and reject functions) as shown here is a bit cleaner and more reusable. Then, you could just do:

this._deferred = new Deferred();

And, later, you can do any of these:

this._deferred.resolve(...);
this._deferred.reject(...);
this._deferred.then(...);
this._deferred.catch(...);
let p = this._deferred.promise;

If you described the overall problem you're trying to solve, we might be able to present a whole different approach to solving the actual problem.

Upvotes: 2

John Morris
John Morris

Reputation: 721

Question was answered by jfriend00 and Jaramanda X in the comments. My second code block was the correct way to do this in my case because the function passed to the Promise constructor is executed before the Promise constructor exits.
That means in my case with no asynchronous code in the function, I can safely assign the promise reference afterwards.

Upvotes: 0

Related Questions