@mcastiello/cancellable-promise
Version:
As the name suggests, this promise can be cancelled while still running.
2 lines (1 loc) • 771 B
JavaScript
var t=0;function e(e){return"__private_"+t+++"_"+e}function r(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var n=/*#__PURE__*/e("controller");class o extends Promise{constructor(t){const e=new AbortController;let o=!1,a=!1;super((r,n)=>{t(t=>{o||(a=!0,r(t))},n),e.signal.addEventListener("abort",()=>{a||(o=!0,r(void 0))})}),Object.defineProperty(this,n,{writable:!0,value:void 0}),r(this,n)[n]=e}cancel(){r(this,n)[n].abort()}static resolve(t){return new o(e=>e(t))}static reject(t){return new o((e,r)=>r(t))}static wait(t){return new o(e=>setTimeout(e,t))}static defer(t){return(...e)=>new o(async function(r,n){try{r(await t(...e))}catch(t){n(t)}})}}export{o as CancellablePromise};