var speculation = function speculation (fn) {
  // Don't cancel by default:
  var cancel = (
    arguments.length > 1 &&
    arguments[1] !== undefined
  ) ? arguments[1] : Promise.reject();

  return new Promise(function (resolve, reject) {
    var noop = function noop () {};

    var onCancel = function onCancel (handleCancel) {
      return cancel.then(
        handleCancel,
        // Filter out the expected "not cancelled" rejection:
        noop
      ).catch(function (e) {
        // Reject the speculation if there's a an error in
        // onCancel:
        return reject(e);
      });
    };

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

module.exports = speculation;

var noop = function noop () {};

// HOF Wraps the native Promise API
// to add take a shouldCancel promise and add
// an onCancel() callback.
var speculation = function speculation (fn) {
  // Don't cancel by default
  var cancel = (
    arguments.length > 1 &&
    arguments[1] !== undefined
  ) ? arguments[1] : Promise.reject();

  return new Promise(function (resolve, reject) {
    var onCancel = function onCancel (handleCancel) {
      return cancel.then(
        handleCancel,
        // Ignore expected cancel rejections:
        noop
      )
      // handle onCancel errors
      .catch(function (e) {
        return reject(e);
      });
    };

    fn(resolve, reject, onCancel);
  });
};

module.exports = speculation;

Add comments & cleanup formatting.
Add comments & cleanup formatting.
