Skip to content

Instantly share code, notes, and snippets.

@brandonweis
Last active August 1, 2019 08:47
Show Gist options
  • Select an option

  • Save brandonweis/1e4d0df727f63947feef5fac9da9fb02 to your computer and use it in GitHub Desktop.

Select an option

Save brandonweis/1e4d0df727f63947feef5fac9da9fb02 to your computer and use it in GitHub Desktop.
export const cancellablePromise = promise => {
let isCanceled = false;
const wrappedPromise = new Promise((resolve, reject) => {
promise.then(
value => (isCanceled ? reject({ isCanceled, value }) : resolve(value)),
error => reject({ isCanceled, error }),
);
});
return {
promise: wrappedPromise,
cancel: () => (isCanceled = true),
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment