UNPKG

await-to-done

Version:

Async await wrapper for easy error handling - zero dependencies

42 lines (35 loc) 1.35 kB
/** * Async result type for async operations */ export declare type AsyncResult<T, E = Error> = Promise<Result<T, E>>; /** * Async await wrapper for easy error handling * * @example * ```ts * const bar = () => new Promise<boolean>((resolve, reject) => {}) * const foo = () => new Promise<string>((resolve, reject) => {}) * ;(async () => { * const [err, data] = await awaitToDone(bar()) * const [err1, data1] = await awaitToDone(bar(), foo()) * const [err2, data2] = await awaitToDone([bar(), foo()]) * })() * ``` * @since 1.0.0 * @author saqqdy * @param promise - Promise * @param promises - Promise rest params * @return - result */ declare function awaitToDone<T, E = Error>(promise: Promise<T>): Promise<Result<T, E>>; declare function awaitToDone<P extends readonly unknown[] | [], E = Error>(promise: PromiseAll<P>): Promise<Result<P, E>>; declare function awaitToDone<T, P extends readonly unknown[] | [], E = Error>(promise: Promise<T>, ...promises: PromiseAll<P>): Promise<Result<[T, ...P], E>>; export default awaitToDone; export declare type PromiseAll<P extends readonly unknown[] | []> = { -readonly [K in keyof P]: Promise<P[K]>; }; /** * Result type for async operations */ export declare type Result<T, E = Error> = [E, undefined] | [null, T]; export { }