UNPKG

@mcastiello/cancellable-promise

Version:

As the name suggests, this promise can be cancelled while still running.

2 lines (1 loc) 2.52 kB
function t(){try{var n=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(n){}return(t=function(){return!!n})()}function n(t){return n=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},n(t)}function e(t,n){return e=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,n){return t.__proto__=n,t},e(t,n)}function r(o){var u="function"==typeof Map?new Map:void 0;return r=function(r){if(null===r||!function(t){try{return-1!==Function.toString.call(t).indexOf("[native code]")}catch(n){return"function"==typeof t}}(r))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==u){if(u.has(r))return u.get(r);u.set(r,o)}function o(){return function(n,r,o){if(t())return Reflect.construct.apply(null,arguments);var u=[null];u.push.apply(u,r);var i=new(n.bind.apply(n,u));return o&&e(i,o.prototype),i}(r,arguments,n(this).constructor)}return o.prototype=Object.create(r.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),e(o,r)},r(o)}function o(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var u=0;function i(t){return"__private_"+u+++"_"+t}function c(t,n){if(!Object.prototype.hasOwnProperty.call(t,n))throw new TypeError("attempted to use private field on non-instance");return t}var f=/*#__PURE__*/i("controller");exports.CancellablePromise=/*#__PURE__*/function(t){var n,r;function u(n){var e,r=new AbortController,u=!1,i=!1;return e=t.call(this,function(t,e){n(function(n){u||(i=!0,t(n))},e),r.signal.addEventListener("abort",function(){i||(u=!0,t(void 0))})})||this,Object.defineProperty(o(e),f,{writable:!0,value:void 0}),c(o(e),f)[f]=r,e}return r=t,(n=u).prototype=Object.create(r.prototype),n.prototype.constructor=n,e(n,r),u.prototype.cancel=function(){c(this,f)[f].abort()},u.resolve=function(t){return new u(function(n){return n(t)})},u.reject=function(t){return new u(function(n,e){return e(t)})},u.wait=function(t){return new u(function(n){return setTimeout(n,t)})},u.defer=function(t){return function(){var n=arguments;return new u(function(e,r){try{var o=function(r,o){try{var u=Promise.resolve(t.apply(void 0,[].slice.call(n))).then(function(t){e(t)})}catch(t){return o(t)}return u&&u.then?u.then(void 0,o):u}(0,function(t){r(t)});return Promise.resolve(o&&o.then?o.then(function(){}):void 0)}catch(t){return Promise.reject(t)}})}},u}(/*#__PURE__*/r(Promise));