Skip to content

Instantly share code, notes, and snippets.

@uhyo
Last active May 26, 2020 12:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uhyo/f97499872fb0685bd5a4b11fa0f55aef to your computer and use it in GitHub Desktop.
Save uhyo/f97499872fb0685bd5a4b11fa0f55aef to your computer and use it in GitHub Desktop.
Sync way of obtaining promise result
Promise.prototype.then = (()=> {
const _then = Promise.prototype.then;
return function(...args) {
_then.call(this, (res) => {
promiseResultMap.set(this, res);
})
this.then = _then;
return _then.apply(this, args);
}
})();
const promiseResultMap = new WeakMap()
function getPromiseValue(p) {
return promiseResultMap.get(p);
}
const sleep = (duration)=> new Promise((resolve) => setTimeout(()=>resolve(0), duration));
(()=> {
const p = sleep(500);
console.log("before", getPromiseValue(p));
p.then(()=> {
console.log("after", getPromiseValue(p));
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment