UNPKG

@xylabs/promise

Version:

Base functionality used throughout XY Labs TypeScript/JavaScript libraries

8 lines (7 loc) 4.66 kB
{ "version": 3, "sources": ["../../src/fulfilled.ts", "../../src/fulfilledValues.ts", "../../src/PromiseEx.ts", "../../src/rejected.ts", "../../src/toPromise.ts"], "sourcesContent": ["/**\n * For use with Promise.allSettled to filter only successful results\n * @param val\n * @returns\n */\nexport const fulfilled = <T>(val: PromiseSettledResult<T>): val is PromiseFulfilledResult<T> => {\n return val.status === 'fulfilled'\n}\n", "/**\n * For use with Promise.allSettled to reduce to only successful result values\n * @example <caption>Casting the initialValue provided to reduce</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce(fulfilledValues, [] as string[])\n * // results === [ 'resolved' ]\n * @example <caption>Providing type parameter to reduce and initialValue type can be inferred</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce<string[]>(fulfilledValues, [])\n * // results === [ 'resolved' ]\n * @param previousValue\n * @param currentValue\n * @returns\n */\nexport const fulfilledValues = <T>(previousValue: T[], currentValue: PromiseSettledResult<T>): T[] => {\n if (currentValue.status === 'fulfilled') previousValue.push(currentValue.value)\n return previousValue\n}\n", "/** A resolve/reject callback used within PromiseEx. */\nexport type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult\n\n/** The executor function passed to the PromiseEx constructor. */\nexport type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void\n\n/** A callback that inspects the attached value and returns whether to cancel the promise. */\nexport type PromiseExValueFunc<V> = (value?: V) => boolean\n\n/**\n * An extended Promise that carries an optional attached value and supports cancellation.\n * The value can be inspected via the `then` or `value` methods to conditionally cancel.\n */\nexport class PromiseEx<T, V = void> extends Promise<T> {\n /** Whether the promise has been cancelled via a value callback. */\n cancelled?: boolean\n private _value?: V\n\n constructor(func: PromiseExFunc<T>, value?: V) {\n super(func)\n this._value = value\n }\n\n // eslint-disable-next-line unicorn/no-thenable\n override then<TResult1 = T, TResult2 = never>(\n onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined,\n onvalue?: (value?: V) => boolean,\n ): Promise<TResult1 | TResult2> {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return super.then(onfulfilled, onrejected)\n }\n\n /**\n * Inspects the attached value via the callback; if it returns true, marks the promise as cancelled.\n * @param onvalue - A callback that receives the attached value and returns whether to cancel.\n * @returns This instance for chaining.\n */\n value(onvalue?: (value?: V) => boolean) {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return this\n }\n}\n", "/**\n * For use with Promise.allSettled to filter only rejected results\n * @param val\n * @returns\n */\nexport const rejected = <T>(val: PromiseSettledResult<T>): val is PromiseRejectedResult => {\n return val.status === 'rejected'\n}\n", "import type { Promisable } from './types.ts'\n\n/**\n * Wraps a value in a Promise if it is not already one.\n * @param value - A value that may or may not be a Promise.\n * @returns A Promise resolving to the value.\n */\nexport function toPromise<T>(value: Promisable<T>): Promise<T> {\n return value instanceof Promise ? value : Promise.resolve(value)\n}\n"], "mappings": ";AAKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW,YAAa,eAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;ACRO,IAAM,YAAN,cAAqC,QAAW;AAAA;AAAA,EAErD;AAAA,EACQ;AAAA,EAER,YAAY,MAAwB,OAAW;AAC7C,UAAM,IAAI;AACV,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA,EAGS,KACP,aACA,YACA,SAC8B;AAC9B,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO,MAAM,KAAK,aAAa,UAAU;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,SAAkC;AACtC,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO;AAAA,EACT;AACF;;;ACzCO,IAAM,WAAW,CAAI,QAA+D;AACzF,SAAO,IAAI,WAAW;AACxB;;;ACAO,SAAS,UAAa,OAAkC;AAC7D,SAAO,iBAAiB,UAAU,QAAQ,QAAQ,QAAQ,KAAK;AACjE;", "names": [] }