UNPKG

result-guard

Version:

Type-safe error handling with discriminated unions and type guards for TypeScript

161 lines (160 loc) 3.71 kB
function E(t) { return t instanceof Error ? t : typeof t == "string" ? new Error(t) : new Error(`Non-error value thrown: ${P(t)}`); } function P(t) { if (t === null) return "null"; if (t === void 0) return "undefined"; try { return JSON.stringify(t); } catch { return String(t); } } function w(t) { return { data: t, error: null, isError: !1 }; } function h(t) { return { data: null, error: t, isError: !0 }; } function m(t) { return t instanceof Error ? t : E(t); } async function v(t) { try { const r = await t; return w(r); } catch (r) { return h(m(r)); } } function I(t) { return Promise.resolve(t).catch((r) => { throw m(r); }); } function y(t, r) { return new Promise((a, o) => { const n = setTimeout(() => { o(new Error(`${r} after ${t}ms`)); }, t); Promise.prototype.cancel = () => clearTimeout(n); }); } function p(t, r, a = "Operation timed out") { const o = y(r, a); return Promise.race([t, o]).finally(() => { var n; (n = o.cancel) == null || n.call(o); }); } function O(t, r, a = {}) { const { errorEvent: o = "error", timeout: n, cleanup: u } = a; return f(async () => { let s = !1; const e = async () => { if (!s) { s = !0, t.removeAllListeners(o); try { await (u == null ? void 0 : u()); } catch { } } }; try { const i = new Promise((g, d) => { t.once(o, d); }), l = [r(), i]; return n && l.push(y(n, "Operation timed out")), await Promise.race(l); } finally { await e(); } }); } async function k(t, r = {}) { const { timeout: a, maxItems: o, onItem: n } = r; return f(async () => { const u = [], s = async () => { var e; try { await ((e = t.return) == null ? void 0 : e.call(t)); } catch { } }; try { const e = (async () => { for (; ; ) { let i; try { i = await t.next(); } catch (c) { throw await s(), c; } if (i.done || n && !await n(i.value) || (u.push(i.value), o && u.length >= o)) break; } return u; })(); return a ? await p(e, a, "Iterator timed out") : await e; } finally { await s(); } }); } function x(t, r = {}) { return f(async () => new Promise((a, o) => { let n = () => { }; if (n = t({ resolve: (e) => { n(), a(e); }, reject: (e) => { n(), o(e); } }) || (() => { }), r.timeout) { const e = setTimeout(() => { n(), o(new Error(`Operation timed out after ${r.timeout}ms`)); }, r.timeout), i = n; n = () => { clearTimeout(e), i(); }; } })); } async function C(t, r = {}) { const { maxConcurrent: a = 1 / 0, timeout: o, stopOnError: n = !1 } = r, u = (e) => f(async () => { const i = e(); return o ? await p(i, o, "Operation timed out") : await i; }); if (a === 1 / 0 || t.length <= a) { const e = await Promise.all(t.map(u)); return n && e.some((i) => i.isError) ? [e.find((i) => i.isError)] : e; } const s = []; for (let e = 0; e < t.length; e += a) { const i = t.slice(e, e + a), c = await Promise.all(i.map(u)); if (s.push(...c), n && c.some((l) => l.isError)) return [c.find((l) => l.isError)]; } return s; } function R(t) { return !t.isError; } function T(t) { return t.isError; } function f(t) { try { const r = t(); return r instanceof Promise ? v(I(r)) : w(r); } catch (r) { return h(m(r)); } } export { C as concurrent, T as isFailure, R as isSuccess, f as tryCatch, x as withCallbacks, O as withEvents, k as withIterator };