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.