{
  "version": 3,
  "sources": ["../Source/Async/index.ts", "../Source/Miscellaneous/Utility.Types.ts", "../Source/Async/Async.ts"],
  "sourcesContent": ["/**\n * Utilities for creating and working with `async` functions and {@link Promise | Promises}.\n *\n * @module @sorrell/utilities/async\n */\n/**\n * @file      index.ts\n * @author    Gage Sorrell <gage@sorrell.sh>\n * @copyright (c) 2026 Gage Sorrell\n * @license   MIT\n */\nexport * from \"./Async.ts\";\nexport * from \"./Async.Types.ts\";\n", "/**\n * @file      Utility.Types.ts\n * @author    Gage Sorrell <gage@sorrell.sh>\n * @copyright (c) 2026 Gage Sorrell\n * @license   MIT\n */\n/**\n * A simple wrapper to allow passing primitives to functions by-reference.\n *\n * @template Type - The type of the value wrapped by this.\n */\nexport type Ref<Type> = {\n    Ref: Type | undefined;\n};\n/**\n * The opposite of {@link NonNullable}: the union of a given {@link Type}, `null`, and `undefined`.\n *\n * @template Type - The nontrivial type in this union.\n */\nexport type TNullable<Type> = Type | null | undefined;\n/** An error that is thrown in default implementations of `abstract` `class`es. */\nexport class AbstractMethodCallError extends Error {\n    public constructor(ClassName?: string) {\n        super(ClassName);\n    }\n}\n/**\n * A \"safe\" intersection of two types, such that if exactly one\n * of the two types is `never`, then this evaluates to the other type.\n * This evaluates to the intersection of the two types iff *both*\n * type parameters.\n *\n * @template LeftType - The first type parameter.\n * @template RightType - The second type parameter.\n */\nexport type TSafeIntersection<LeftType, RightType> = [\n    LeftType\n] extends [\n    never\n] ? [\n    RightType\n] extends [\n    never\n] ? never : RightType : [\n    RightType\n] extends [\n    never\n] ? LeftType : (LeftType & RightType);\n", "/**\n * @file      Async.ts\n * @author    Gage Sorrell <gage@sorrell.sh>\n * @copyright (c) 2026 Gage Sorrell\n * @license   MIT\n */\nimport { AbstractMethodCallError, type TNullable } from \"../Miscellaneous/Utility.Types.ts\";\nimport type { TOnFulfilled, TOnRejected, TPromiseCtorArgument, TTryResult, TTrySource } from \"./Async.Types.ts\";\nimport type { TFunction } from \"../Functional/Functional.Types.ts\";\n/**\n * Implements \"Errors-as-values\" for `async` functions and `Promise<DataType>`s.\n *\n * If the `async` function or `Promise<DataType>` returns/resolves, then the `Error`\n * property of the returned object will be `undefined`, and the result of the\n * `async` function or `Promise<DataType>` will reside in the `Data` property of the\n * returned object.\n *\n * Similarly, if the `async` function or `Promise<DataType>` throws, then the `Data`\n * property of the returned object is `undefined`, and the `Error` property is what\n * was thrown to the `catch` block.\n *\n * @template DataType - The type of the data that the `async` function or `Promise<DataType>`.\n * @param Source - The `async` function or `Promise` to evaluate.\n *\n * @returns {TTryResult<DataType>} A {@link TTryResult} containing either the result\n * ({@link TTryResult.Data}) or error {@link TTryResult.Error}.\n *\n * @example\n * // With an `async` function,\n * async function GetName(): Promise<string> { ... }\n * // Or, with a `Promise`,\n * const GetName: Promise<string> = new Promise<string>(...);\n *\n * // For both of the above cases, `Try` behaves the same:\n * const { Data: Name, Error: NameError } = await Try(GetName);\n * // If `GetName` returned,\n * // `typeof Name` <- `\"string\"`\n * // `typeof NameError` <- `\"undefined\"`\n * // If `GetName` threw,\n * // `typeof Name` <- `\"undefined\"`\n * // `NameError !== undefined` <- `true` (Unless `GetName` threw `undefined`).\n */\nexport async function Try<DataType>(Source: TTrySource<DataType>): Promise<TTryResult<DataType>> {\n    try {\n        const Data: DataType = typeof Source === \"function\"\n            ? await Source()\n            : await Source;\n        return {\n            Data,\n            Error: undefined\n        };\n    }\n    catch (ErrorValue: unknown) {\n        return {\n            Data: undefined,\n            Error: ErrorValue\n        };\n    }\n}\n/**\n * A cleaner way of using `Array.prototype.map` with an `async` function.\n *\n * @template ArgumentElementType - The type of the given {@link Elements}.\n * @template ReturnElementType - The type of the `Array` returned by this.\n *\n * @param Elements - The `Array` that will be transformed.\n * @param Mapper - The function that maps each {@link ArgumentElementType}\n * to a {@link ReturnElementType}.\n *\n * @returns {Array<ReturnElementType>} An `Array` of {@link ReturnElementType}.\n *\n * @example\n * In an `async` function,\n * ```typescript\n * const ArgumentElements: Array<ArgumentElementType> = [ ... ];\n * const ToReturnElement = async (Element: ArgumentElementType): Promise<ReturnElementType> => ...;\n * const ReturnElements: Array<ReturnElementType> = await Map(ArgumentElements, ToReturnElement);\n * ```\n */\nexport async function Map<ArgumentElementType, ReturnElementType>(Elements: Array<ArgumentElementType>, Mapper: ((Element: ArgumentElementType) => Promise<ReturnElementType>)): Promise<Array<ReturnElementType>> {\n    const MapperWrapped = async (Element: ArgumentElementType): Promise<ReturnElementType> => {\n        return Mapper(Element);\n    };\n    const Out: Array<Promise<ReturnElementType>> = Elements.map(MapperWrapped);\n    return await Promise.all(Out);\n}\n/**\n * An abstract implementation of {@link PromiseLike}.  This exists only to maintain the\n * project's naming convention.\n */\nexport abstract class TPromiseLike<ResolveType> implements PromiseLike<ResolveType> {\n    public then<ResultType = ResolveType, RejectType = never>(_OnFulfilled?: TOnFulfilled<ResolveType, ResultType>, _OnRejected?: TOnRejected<RejectType>): TPromiseLike<ResultType | RejectType> {\n        throw new AbstractMethodCallError(\"TPromiseLike\");\n    }\n}\n/**\n * The {@link TypeError} that is thrown by the constructor of {@link TPromise}\n * iff it receives an argument that is not a {@link TPromiseCtorArgument}.\n * It does not provide any additional information (in particular, it does not\n * add any properties, and it does not set any properties inherited from\n * {@link TypeError}).\n *\n * @note While the argument type of {@link TPromise}'s constructor is checked at runtime,\n * the type checker should prevent the error described by this from happening.\n */\nexport class FPromiseInstantiationError extends TypeError {\n    public constructor() {\n        super();\n    }\n}\n/**\n * A `then`-able wrapper of {@link Promise}.\n *\n * @template ResolveType - The type to which this resolves, if it does resolve.\n */\nexport class TPromise<ResolveType> extends TPromiseLike<ResolveType> implements PromiseLike<ResolveType> {\n    public constructor(Argument: TPromiseCtorArgument<ResolveType>) {\n        super();\n        if (typeof Argument === \"function\") {\n            this.Underlying = new Promise<ResolveType>(Argument);\n        }\n        else {\n            if (Argument instanceof TPromise) {\n                this.Underlying = Argument.Raw();\n            }\n            else if (Argument instanceof Promise) {\n                this.Underlying = Argument;\n            }\n        }\n        throw new FPromiseInstantiationError();\n    }\n    private readonly Underlying: Promise<ResolveType>;\n    /**\n     * This class's extension of {@link Promise!then}.\n     *\n     * @template ResultType - The type to which the value underlying the {@link TPromise} returned by this\n     * will resolve, if the {@link TPromise} resolves.\n     *\n     * @template RejectType - The type to which the value underlying the {@link TPromise} returned by this\n     * will reject, if the {@link TPromise} rejects.\n     *\n     * @param OnFulfilled - The callback that is called when the underlying {@link Promise} resolves,\n     * if it resolves.\n     *\n     * @param OnRejected - The callback that is called when the underlying {@link Promise} rejects,\n     * if it rejects.\n     *\n     * @returns {TPromise<ResultType | RejectType>} A new {@link TPromise} of the result of the\n     * given callbacks.\n     *\n     * @example\n     * ```typescript\n     * const Example: TPromise<string> = new TPromise<string>((Resolve, Reject) =>\n     * {\n     *     if (Math.random() < 0.5)\n     *     {\n     *         Resolve(\"Success\");\n     *     }\n     *     else\n     *     {\n     *         Reject(\"Error\");\n     *     }\n     * });\n     *\n     * Example.then(\n     *     (Value: string): void =>\n     *     {\n     *         // `Value` <- `\"Success\"`\n     *     },\n     *     (Reason: unknown): void =>\n     *     {\n     *         // `Reason` <- `\"Error\"`\n     *     }\n     * );\n     * ```\n     */\n    public Then<ResultType = ResolveType, RejectType = never>(OnFulfilled: TOnFulfilled<ResolveType, ResultType>, OnRejected?: TOnRejected<RejectType>): TPromise<ResultType | RejectType> {\n        return new TPromise<ResultType | RejectType>(this.Underlying.then(OnFulfilled, OnRejected));\n    }\n    public override then<ResultType = ResolveType, RejectType = never>(OnFulfilled?: TOnFulfilled<ResolveType, ResultType>, OnRejected?: TOnRejected<RejectType>): TPromise<ResultType | RejectType> {\n        return new TPromise<ResultType | RejectType>(this.Underlying.then(OnFulfilled, OnRejected));\n    }\n    public Catch<ResultType = never>(OnRejected?: TNullable<TOnRejected<ResolveType>>): TPromiseLike<ResolveType | ResultType> {\n        return new TPromise<ResolveType | ResultType>(this.Underlying.catch(OnRejected));\n    }\n    public Finally(OnFinally?: TNullable<TFunction>): TPromise<ResolveType> {\n        return new TPromise(this.Underlying.finally(OnFinally));\n    }\n    public static Resolve<ResolveType>(Value: ResolveType | PromiseLike<ResolveType>): TPromise<Awaited<ResolveType>> {\n        return new TPromise(Promise.resolve(Value));\n    }\n    public static Reject<ResolveType = unknown>(Reason?: unknown): TPromise<ResolveType> {\n        return new TPromise(Promise.reject<ResolveType>(Reason));\n    }\n    public Raw(): Promise<ResolveType> {\n        return this.Underlying;\n    }\n}\n;\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqBO,IAAM,0BAAN,cAAsC,MAAM;AAAA,EACxC,YAAY,WAAoB;AACnC,UAAM,SAAS;AAAA,EACnB;AACJ;;;ACiBA,eAAsB,IAAc,QAA6D;AAC7F,MAAI;AACA,UAAM,OAAiB,OAAO,WAAW,aACnC,MAAM,OAAO,IACb,MAAM;AACZ,WAAO;AAAA,MACH;AAAA,MACA,OAAO;AAAA,IACX;AAAA,EACJ,SACO,YAAqB;AACxB,WAAO;AAAA,MACH,MAAM;AAAA,MACN,OAAO;AAAA,IACX;AAAA,EACJ;AACJ;AAqBA,eAAsB,IAA4C,UAAsC,QAA2G;AAC/M,QAAM,gBAAgB,OAAO,YAA6D;AACtF,WAAO,OAAO,OAAO;AAAA,EACzB;AACA,QAAM,MAAyC,SAAS,IAAI,aAAa;AACzE,SAAO,MAAM,QAAQ,IAAI,GAAG;AAChC;AAKO,IAAe,eAAf,MAA6E;AAAA,EACzE,KAAmD,cAAsD,aAA8E;AAC1L,UAAM,IAAI,wBAAwB,cAAc;AAAA,EACpD;AACJ;AAWO,IAAM,6BAAN,cAAyC,UAAU;AAAA,EAC/C,cAAc;AACjB,UAAM;AAAA,EACV;AACJ;AAMO,IAAM,WAAN,MAAM,kBAA8B,aAA8D;AAAA,EAC9F,YAAY,UAA6C;AAC5D,UAAM;AAcV,wBAAiB;AAbb,QAAI,OAAO,aAAa,YAAY;AAChC,W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