Ordinarily, a Promise
is constructed and used like this:
new Promise((resolve, reject) => {
const obj = new MyEventEmitter();
obj.onsuccess = (event) => { resolve(event.result); };
obj.onerror = (event) => { reject(event.error); };
});
But recently, I have been doing something like this to take the resolver outside the executor callback for the sake of flexibility:
let outsideResolve;
let outsideReject;
new Promise((resolve, reject) => {
outsideResolve = resolve;
outsideReject = reject;
});
And later:
onClick = function() {
outsideResolve();
}
This works fine, but is there an easier way to do this? If not, is this a good practice?
simple:
var promiseResolve, promiseReject;
var promise = new Promise(function(resolve, reject){
promiseResolve = resolve;
promiseReject = reject;
});
promiseResolve();