better-promises
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,2 +0,2 @@ | ||
var enhancedPromise=function(n){"use strict";var P=Object.defineProperty;var B=(n,r,i)=>r in n?P(n,r,{enumerable:!0,configurable:!0,writable:!0,value:i}):n[r]=i;var m=(n,r,i)=>B(n,typeof r!="symbol"?r+"":r,i);class r extends Error{static is(s){return s instanceof r}constructor(s){super("Execution was aborted",{cause:s}),Object.setPrototypeOf(this,r.prototype)}}class i extends Error{static is(s){return s instanceof i}constructor(){super("Execution was canceled"),Object.setPrototypeOf(this,i.prototype)}}class d extends Error{static is(s){return s instanceof d}constructor(s,e){super(`Timeout reached: ${s}ms`,{cause:e}),Object.setPrototypeOf(this,d.prototype)}}const b=Symbol("Resolved");function F(a){return a===b}function g(a,s){return a.reject=s.reject,a}class l extends Promise{constructor(e,t){let o,u;super((v,w)=>{let p,h;typeof e=="function"?(p=e,h=t):h=e,h||(h={});const{abortSignal:f}=h;if(f&&f.aborted)return w(new r(f.reason));const j=[],L=c=>(...M)=>(j.forEach(O=>O()),c(...M)),E=new AbortController,{signal:T}=E;if(u=L(c=>{w(c),E.abort(c)}),o=L(c=>{v(c),E.abort(b)}),f){const c=()=>{u(new r(f.reason))};f.addEventListener("abort",c),j.push(()=>{f.removeEventListener("abort",c)})}const{timeout:S}=h;if(S){const c=setTimeout(()=>{u(new d(S))},S);j.push(()=>{clearTimeout(c)})}p&&p(o,u,T)});m(this,"reject");this.reject=u}static withFn(e,t){return new l((o,u,v)=>{o(e(v))},t)}static resolve(e){return new l(t=>{t(e)})}static reject(e){return new l((t,o)=>{o(e)})}cancel(){this.reject(new r(new i))}catch(e){return this.then(void 0,e)}finally(e){return g(super.finally(e),this)}then(e,t){return g(super.then(e,t),this)}}function R(a,s){return a.resolve=s.resolve,a}class y extends l{constructor(e,t){let o,u;typeof e=="function"?(o=e,u=t):u=e;let v;super((w,p,h)=>{v=w,o&&o(w,p,h)},u);m(this,"resolve");this.resolve=v}static withFn(e,t){return this.resolve().then(()=>l.withFn(e,t))}static resolve(e){return new y(t=>{t(e)})}static reject(e){return new y((t,o)=>{o(e)})}catch(e){return this.then(void 0,e)}finally(e){return R(super.finally(e),this)}then(e,t){return R(super.then(e,t),this)}}return n.AbortError=r,n.CancelablePromise=l,n.CanceledError=i,n.ManualPromise=y,n.RESOLVED_SYMBOL=b,n.TimeoutError=d,n.isResolved=F,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),n}({}); | ||
var enhancedPromise=function(n){"use strict";var B=Object.defineProperty;var D=(n,r,i)=>r in n?B(n,r,{enumerable:!0,configurable:!0,writable:!0,value:i}):n[r]=i;var S=(n,r,i)=>D(n,typeof r!="symbol"?r+"":r,i);class r extends Error{static is(s){return s instanceof r}constructor(s){super("Execution was aborted",{cause:s}),Object.setPrototypeOf(this,r.prototype)}}class i extends Error{static is(s){return s instanceof i}constructor(){super("Execution was canceled"),Object.setPrototypeOf(this,i.prototype)}}class w extends Error{static is(s){return s instanceof w}constructor(s,t){super(`Timeout reached: ${s}ms`,{cause:t}),Object.setPrototypeOf(this,w.prototype)}}const b=Symbol("Resolved");function P(a){return a===b}function g(a,s){return a.reject=s.reject,a}class l extends Promise{constructor(t,e){let o,u;super((v,d)=>{let p,h;typeof t=="function"?(p=t,h=e):h=t,h||(h={});const{abortSignal:f}=h;if(f&&f.aborted)return d(new r(f.reason));const E=[],L=c=>(...M)=>(E.forEach(O=>O()),c(...M)),j=new AbortController,{signal:T}=j;if(u=L(c=>{d(c),j.abort(c)}),o=L(c=>{v(c),j.abort(b)}),f){const c=()=>{u(new r(f.reason))};f.addEventListener("abort",c),E.push(()=>{f.removeEventListener("abort",c)})}const{timeout:m}=h;if(m){const c=setTimeout(()=>{u(new w(m))},m);E.push(()=>{clearTimeout(c)})}const F=p&&p(o,u,T);F instanceof Promise&&F.catch(d)});S(this,"reject");this.reject=u}static withFn(t,e){return new l((o,u,v)=>{o(t(v))},e)}static resolve(t){return new l(e=>{e(t)})}static reject(t){return new l((e,o)=>{o(t)})}cancel(){this.reject(new r(new i))}catch(t){return this.then(void 0,t)}finally(t){return g(super.finally(t),this)}then(t,e){return g(super.then(t,e),this)}}function R(a,s){return a.resolve=s.resolve,a}class y extends l{constructor(t,e){let o,u;typeof t=="function"?(o=t,u=e):u=t;let v;super((d,p,h)=>{v=d,o&&o(d,p,h)},u);S(this,"resolve");this.resolve=v}static withFn(t,e){return this.resolve().then(()=>l.withFn(t,e))}static resolve(t){return new y(e=>{e(t)})}static reject(t){return new y((e,o)=>{o(t)})}catch(t){return this.then(void 0,t)}finally(t){return R(super.finally(t),this)}then(t,e){return R(super.then(t,e),this)}}return n.AbortError=r,n.CancelablePromise=l,n.CanceledError=i,n.ManualPromise=y,n.RESOLVED_SYMBOL=b,n.TimeoutError=w,n.isResolved=P,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),n}({}); | ||
//# sourceMappingURL=index.iife.js.map |
@@ -1,10 +0,10 @@ | ||
var T = Object.defineProperty; | ||
var B = (r, e, t) => e in r ? T(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t; | ||
var b = (r, e, t) => B(r, typeof e != "symbol" ? e + "" : e, t); | ||
class a extends Error { | ||
var B = Object.defineProperty; | ||
var D = (s, e, t) => e in s ? B(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t; | ||
var b = (s, e, t) => D(s, typeof e != "symbol" ? e + "" : e, t); | ||
class h extends Error { | ||
static is(e) { | ||
return e instanceof a; | ||
return e instanceof h; | ||
} | ||
constructor(e) { | ||
super("Execution was aborted", { cause: e }), Object.setPrototypeOf(this, a.prototype); | ||
super("Execution was aborted", { cause: e }), Object.setPrototypeOf(this, h.prototype); | ||
} | ||
@@ -28,13 +28,13 @@ } | ||
} | ||
const S = Symbol("Resolved"); | ||
function I(r) { | ||
return r === S; | ||
const m = Symbol("Resolved"); | ||
function V(s) { | ||
return s === m; | ||
} | ||
function g(r, e) { | ||
return r.reject = e.reject, r; | ||
function R(s, e) { | ||
return s.reject = e.reject, s; | ||
} | ||
class h extends Promise { | ||
class f extends Promise { | ||
constructor(t, n) { | ||
let s, c; | ||
super((l, f) => { | ||
let r, c; | ||
super((l, a) => { | ||
let p, i; | ||
@@ -44,11 +44,11 @@ typeof t == "function" ? (p = t, i = n) : i = t, i || (i = {}); | ||
if (u && u.aborted) | ||
return f(new a(u.reason)); | ||
const d = [], x = (o) => (...L) => (d.forEach((m) => m()), o(...L)), y = new AbortController(), { signal: F } = y; | ||
return a(new h(u.reason)); | ||
const d = [], x = (o) => (...L) => (d.forEach((T) => T()), o(...L)), y = new AbortController(), { signal: F } = y; | ||
if (c = x((o) => { | ||
f(o), y.abort(o); | ||
}), s = x((o) => { | ||
l(o), y.abort(S); | ||
a(o), y.abort(o); | ||
}), r = x((o) => { | ||
l(o), y.abort(m); | ||
}), u) { | ||
const o = () => { | ||
c(new a(u.reason)); | ||
c(new h(u.reason)); | ||
}; | ||
@@ -68,3 +68,4 @@ u.addEventListener("abort", o), d.push(() => { | ||
} | ||
p && p(s, c, F); | ||
const g = p && p(r, c, F); | ||
g instanceof Promise && g.catch(a); | ||
}); | ||
@@ -89,8 +90,8 @@ /** | ||
static withFn(t, n) { | ||
return new h((s, c, l) => { | ||
s(t(l)); | ||
return new f((r, c, l) => { | ||
r(t(l)); | ||
}, n); | ||
} | ||
static resolve(t) { | ||
return new h((n) => { | ||
return new f((n) => { | ||
n(t); | ||
@@ -103,4 +104,4 @@ }); | ||
static reject(t) { | ||
return new h((n, s) => { | ||
s(t); | ||
return new f((n, r) => { | ||
r(t); | ||
}); | ||
@@ -113,3 +114,3 @@ } | ||
cancel() { | ||
this.reject(new a(new v())); | ||
this.reject(new h(new v())); | ||
} | ||
@@ -126,3 +127,3 @@ /** | ||
finally(t) { | ||
return g(super.finally(t), this); | ||
return R(super.finally(t), this); | ||
} | ||
@@ -133,15 +134,15 @@ /** | ||
then(t, n) { | ||
return g(super.then(t, n), this); | ||
return R(super.then(t, n), this); | ||
} | ||
} | ||
function R(r, e) { | ||
return r.resolve = e.resolve, r; | ||
function S(s, e) { | ||
return s.resolve = e.resolve, s; | ||
} | ||
class E extends h { | ||
class E extends f { | ||
constructor(t, n) { | ||
let s, c; | ||
typeof t == "function" ? (s = t, c = n) : c = t; | ||
let r, c; | ||
typeof t == "function" ? (r = t, c = n) : c = t; | ||
let l; | ||
super((f, p, i) => { | ||
l = f, s && s(f, p, i); | ||
super((a, p, i) => { | ||
l = a, r && r(a, p, i); | ||
}, c); | ||
@@ -161,3 +162,3 @@ /** | ||
static withFn(t, n) { | ||
return this.resolve().then(() => h.withFn(t, n)); | ||
return this.resolve().then(() => f.withFn(t, n)); | ||
} | ||
@@ -173,4 +174,4 @@ static resolve(t) { | ||
static reject(t) { | ||
return new E((n, s) => { | ||
s(t); | ||
return new E((n, r) => { | ||
r(t); | ||
}); | ||
@@ -188,3 +189,3 @@ } | ||
finally(t) { | ||
return R(super.finally(t), this); | ||
return S(super.finally(t), this); | ||
} | ||
@@ -195,14 +196,14 @@ /** | ||
then(t, n) { | ||
return R(super.then(t, n), this); | ||
return S(super.then(t, n), this); | ||
} | ||
} | ||
export { | ||
a as AbortError, | ||
h as CancelablePromise, | ||
h as AbortError, | ||
f as CancelablePromise, | ||
v as CanceledError, | ||
E as ManualPromise, | ||
S as RESOLVED_SYMBOL, | ||
m as RESOLVED_SYMBOL, | ||
w as TimeoutError, | ||
I as isResolved | ||
V as isResolved | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "better-promises", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "JavaScript's Promise extensions you may find useful during development.", | ||
@@ -5,0 +5,0 @@ "author": "Vladislav Kibenko <wolfram.deus@gmail.com>", |
@@ -42,3 +42,3 @@ # `better-promises` | ||
// never be resolved. | ||
const p = new CancelablePromise(); | ||
const promise = new CancelablePromise(); | ||
@@ -48,3 +48,3 @@ // Using the classic Promise executor with the additional | ||
// was resolved or rejected externally. | ||
const p2 = new CancelablePromise((res, rej, abortSignal) => { | ||
const promise2 = new CancelablePromise((res, rej, abortSignal) => { | ||
// .. | ||
@@ -54,8 +54,8 @@ }); | ||
// Using only options. All options are optional. | ||
const c = new AbortController(); | ||
const p3 = new CancelablePromise({ | ||
const controller = new AbortController(); | ||
const promise3 = new CancelablePromise({ | ||
// Abort signal to let the promise know, the execution | ||
// should be aborted. If the signal was aborted, the | ||
// promise will be rejected with the AbortError, | ||
abortSignal: c.signal, | ||
abortSignal: controller.signal, | ||
// Execution timeout. When timeout was reached, the | ||
@@ -67,6 +67,6 @@ // promise will be rejected with the TimeoutError. | ||
// Using the executor and options. | ||
const c2 = new AbortController(); | ||
const p4 = new CancelablePromise((res, rej, abortSignal) => { | ||
const controller2 = new AbortController(); | ||
const promise4 = new CancelablePromise((res, rej, abortSignal) => { | ||
// .. | ||
}, { abortSignal: c.signal, timeout: 3000 }) | ||
}, { abortSignal: controller.signal, timeout: 3000 }) | ||
``` | ||
@@ -77,2 +77,29 @@ | ||
### Passing Async Executor | ||
Unlike JavaScript's `Promise` executor, the executor passed to the `CancelablePromise` constructor | ||
is allowed to be a function that returns a `Promise`. If the returned promise is rejected, | ||
the `CancelablePromise` will also be rejected. | ||
The following code will not work as expected because the executor returns a promise that gets | ||
rejected: | ||
```ts | ||
const promise = new Promise(async (_, rej) => { | ||
throw new Error('Oops!'); | ||
}) | ||
.catch(e => console.error('Handled:', e)); | ||
// Uncaught (in promise) Error: Oops! | ||
``` | ||
However, the `CancelablePromise` class can handle this type of error: | ||
```ts | ||
const promise = new CancelablePromise(async (_, rej) => { | ||
throw new Error('Oops!'); | ||
}) | ||
.catch(e => console.error('Handled:', e)); | ||
// Handled: Error('Oops!') | ||
``` | ||
### `withFn` | ||
@@ -84,22 +111,22 @@ | ||
```ts | ||
const c2 = new AbortController(); | ||
const p = CancelablePromise.withFn((abortSignal) => { | ||
const controller = new AbortController(); | ||
const promise = CancelablePromise.withFn((abortSignal) => { | ||
return 'Resolved!'; | ||
}, { | ||
abortSignal: c.signal, | ||
abortSignal: controller.signal, | ||
timeout: 3000, | ||
}); | ||
p.then(console.log); // Output: 'Resolved!' | ||
promise.then(console.log); // Output: 'Resolved!' | ||
const p2 = CancelablePromise.withFn(() => { | ||
const promise2 = CancelablePromise.withFn(() => { | ||
throw new Error('Nah :('); | ||
}); | ||
p2.catch(console.error); // Output: Error('Nah :(') | ||
promise2.catch(console.error); // Output: Error('Nah :(') | ||
const p3 = CancelablePromise.withFn(async () => { | ||
const promise3 = CancelablePromise.withFn(async () => { | ||
const r = await fetch('...'); | ||
return r.json(); | ||
}); | ||
// p3 resolves with the fetched JSON body | ||
// promise3 resolves with the fetched JSON body | ||
``` | ||
@@ -171,5 +198,5 @@ | ||
const p = new ManualPromise(); | ||
p.resolve('Done!'); | ||
p.then(console.log); // 'Done!' | ||
const promise = new ManualPromise(); | ||
promise.resolve('Done!'); | ||
promise.then(console.log); // 'Done!' | ||
``` | ||
@@ -176,0 +203,0 @@ |
@@ -155,3 +155,10 @@ import { | ||
executor && executor(resolve, reject, controllerSignal); | ||
const result = executor && executor(resolve, reject, controllerSignal); | ||
// If a promise was returned, we want to handle its rejection because the JS Promise | ||
// will not do it for us. | ||
// Not catching the promise rejection this way, an unhandled promise rejection error will | ||
// be thrown. | ||
if (result instanceof Promise) { | ||
result.catch(rej); | ||
} | ||
}); | ||
@@ -158,0 +165,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
104721
1074
218