Using ES2015, can I resolve a promise from the outside i.e. trigger a resolution after its creation?
Like
const promise = new Promise();
promise.then(() => foo());
promise.resolve(); // foo() gets executed
Using ES2015, can I resolve a promise from the outside i.e. trigger a resolution after its creation?
Like
const promise = new Promise();
promise.then(() => foo());
promise.resolve(); // foo() gets executed
Yes you can.
let resolvePromise = null;
const promise = new Promise(resolve => resolvePromise = resolve);
promise.then(foo => console.log(foo));
resolvePromise('bar');
Sure we can. just take the reference of the function outside and call it. Since functions are object(which are stored as reference in variables) we can call resolve function from outside after taking its reference outside.
var a;
function b(){
var c = new Promise( function(resolve, reject){
a=resolve;
});
return c;
}
b().then((data) => {
console.log(data);
}
);
a("hai");