await-to-done
Version:
Async await wrapper for easy error handling
32 lines (27 loc) • 1.15 kB
TypeScript
/**
* 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<[E, undefined] | [null, T]>;
declare function awaitToDone<P extends readonly unknown[] | [], E = Error>(promise: PromiseAll<P>): Promise<[E, undefined] | [null, P]>;
declare function awaitToDone<T, P extends readonly unknown[] | [], E = Error>(promise: Promise<T>, ...promises: PromiseAll<P>): Promise<[E, undefined] | [null, [T, ...P]]>;
export default awaitToDone;
export declare type PromiseAll<P extends readonly unknown[] | []> = {
-readonly [K in keyof P]: Promise<P[K]>;
};
export { }