import { TypedValue } from '@xylabs/typeof'; export { isPromise } from '@xylabs/typeof'; /** * For use with Promise.allSettled to filter only successful results * @param val * @returns */ declare const fulfilled: (val: PromiseSettledResult) => val is PromiseFulfilledResult; /** * For use with Promise.allSettled to reduce to only successful result values * @example Casting the initialValue provided to reduce * const resolved = Promise.resolve('resolved') * const rejected = Promise.reject('rejected') * const settled = await Promise.allSettled([resolved, rejected]) * const results = settled.reduce(fulfilledValues, [] as string[]) * // results === [ 'resolved' ] * @example Providing type parameter to reduce and initialValue type can be inferred * const resolved = Promise.resolve('resolved') * const rejected = Promise.reject('rejected') * const settled = await Promise.allSettled([resolved, rejected]) * const results = settled.reduce(fulfilledValues, []) * // results === [ 'resolved' ] * @param previousValue * @param currentValue * @returns */ declare const fulfilledValues: (previousValue: T[], currentValue: PromiseSettledResult) => T[]; /** A resolve/reject callback used within PromiseEx. */ type PromiseExSubFunc = (value: T) => TResult; /** The executor function passed to the PromiseEx constructor. */ type PromiseExFunc = (resolve?: PromiseExSubFunc, reject?: PromiseExSubFunc) => void; /** A callback that inspects the attached value and returns whether to cancel the promise. */ type PromiseExValueFunc = (value?: V) => boolean; /** * An extended Promise that carries an optional attached value and supports cancellation. * The value can be inspected via the `then` or `value` methods to conditionally cancel. */ declare class PromiseEx extends Promise { /** Whether the promise has been cancelled via a value callback. */ cancelled?: boolean; private _value?; constructor(func: PromiseExFunc, value?: V); then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | null | undefined, onrejected?: ((reason: unknown) => TResult2 | PromiseLike) | null | undefined, onvalue?: (value?: V) => boolean): Promise; /** * Inspects the attached value via the callback; if it returns true, marks the promise as cancelled. * @param onvalue - A callback that receives the attached value and returns whether to cancel. * @returns This instance for chaining. */ value(onvalue?: (value?: V) => boolean): this; } /** * For use with Promise.allSettled to filter only rejected results * @param val * @returns */ declare const rejected: (val: PromiseSettledResult) => val is PromiseRejectedResult; /** A value that may be a Promise, PromiseEx, or a plain synchronous value. */ type Promisable = PromiseEx | Promise | T; /** A Promisable that resolves to an array. */ type PromisableArray = Promisable; /** A Promisable that may resolve to undefined. */ type OptionalPromisable = Promisable; /** A Promisable array where elements may be undefined. */ type OptionalPromisableArray = PromisableArray; /** A Promisable that may resolve to null. */ type NullablePromisable = Promisable; /** A Promisable array where elements may be null. */ type NullablePromisableArray = PromisableArray; /** @description Used to document promises that are being used as Mutexes */ type AsyncMutex = Promise; /** An interface representing any thenable (promise-like) object. */ interface PromiseType { then: () => unknown; } /** Any non-promise typed value, excluding thenables. */ type AnyNonPromise = Exclude; /** * Wraps a value in a Promise if it is not already one. * @param value - A value that may or may not be a Promise. * @returns A Promise resolving to the value. */ declare function toPromise(value: Promisable): Promise; export { PromiseEx, fulfilled, fulfilledValues, rejected, toPromise }; export type { AnyNonPromise, AsyncMutex, NullablePromisable, NullablePromisableArray, OptionalPromisable, OptionalPromisableArray, Promisable, PromisableArray, PromiseExFunc, PromiseExSubFunc, PromiseExValueFunc, PromiseType };