ericelliott / speculation

JavaScript promises are made to be broken. Speculations are cancellable promises.
MIT License
197 stars 9 forks source link

[Fixes #4] Errors thrown from onCancel() are swallowed. #5

Closed ericelliott closed 7 years ago

ericelliott commented 7 years ago

Handle the normal cancel error with a noop and then catch the success handler's error with .catch():

const speculation = (
  fn, cancel = Promise.reject('Cancelled')
) => new Promise((resolve, reject) => {
  const noop = () => {};

  const handleCancel = (
    onCancel
  ) => cancel.then(
    onCancel,
    noop
  ).catch(e => reject(e));

  return fn(resolve, reject, handleCancel);
});