Skip to content

Instantly share code, notes, and snippets.

@marlun78
Created October 26, 2017 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marlun78/e0a24441e00a69b626606c58f02256c8 to your computer and use it in GitHub Desktop.
Save marlun78/e0a24441e00a69b626606c58f02256c8 to your computer and use it in GitHub Desktop.
An idea for promise cancelation (it doesn’t really cancel the promise, just make sure it never resolves or rejects)
// Just an idea, completely untested!
function makeCancelable(promise) {
let canceled = false;
const proxy = new Promise((resolve, reject) => {
promise.then(
(value) => canceled === false && resolve(value),
(error) => canceled === false && reject(error)
);
});
proxy.cancel = () => canceled = true;
return proxy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment