UNPKG

@ayonli/jsext

Version:

A JavaScript extension package for building strong and modern applications.

139 lines (138 loc) 3.92 kB
/** * Calls a function safely and return errors when captured. * @module */ /** * Invokes an async generator function and renders its yield value and result in * an `[err, val]` tuple. * * @example * ```ts * import _try from "@ayonli/jsext/try"; * * const iter = _try(async function* () { * // do something that may fail * }); * * for await (const [err, val] of iter) { * if (err) { * console.error("something went wrong:", err); * } else { * console.log("current value:", val); * } * } * ``` */ export default function _try<E = unknown, T = any, A extends any[] = any[], TReturn = any, TNext = unknown>(fn: (...args: A) => AsyncGenerator<T, TReturn, TNext>, ...args: A): AsyncGenerator<[E, T], [E, TReturn], TNext>; /** * Invokes a generator function and renders its yield value and result in an * `[err, val]` tuple. * * @example * ```ts * import _try from "@ayonli/jsext/try"; * * const iter = _try(function* () { * // do something that may fail * }); * * for (const [err, val] of iter) { * if (err) { * console.error("something went wrong:", err); * } else { * console.log("current value:", val); * } * } * ``` */ export default function _try<E = unknown, T = any, A extends any[] = any[], TReturn = any, TNext = unknown>(fn: (...args: A) => Generator<T, TReturn, TNext>, ...args: A): Generator<[E, T], [E, TReturn], TNext>; /** * Invokes an async function and renders its result in an `[err, res]` tuple. * * @example * ```ts * import _try from "@ayonli/jsext/try"; * import axios from "axios"; * * let [err, res] = await _try(async () => { * return await axios.get("https://example.org"); * }); * * if (err) { * res = (err as any)["response"]; * } * ``` */ export default function _try<E = unknown, R = any, A extends any[] = any[]>(fn: (...args: A) => Promise<R>, ...args: A): Promise<[E, R]>; /** * Invokes a function and renders its result in an `[err, res]` tuple. * * @example * ```ts * import _try from "@ayonli/jsext/try"; * * const [err, res] = _try(() => { * // do something that may fail * }); * ``` */ export default function _try<E = unknown, R = any, A extends any[] = any[]>(fn: (...args: A) => R, ...args: A): [E, R]; /** * Resolves an async generator and renders its yield value and result in an * `[err, val]` tuple. * * @example * ```ts * import _try from "@ayonli/jsext/try"; * * async function* gen() { * // do something that may fail * } * * for await (const [err, val] of _try(gen())) { * if (err) { * console.error("something went wrong:", err); * } else { * console.log("current value:", val); * } * } * ``` */ export default function _try<E = unknown, T = any, TReturn = any, TNext = unknown>(gen: AsyncGenerator<T, TReturn, TNext>): AsyncGenerator<[E, T], [E, TReturn], TNext>; /** * Resolves a generator and renders its yield value and result in an `[err, val]` * tuple. * * @example * ```ts * import _try from "@ayonli/jsext/try"; * import { range } from "@ayonli/jsext/number"; * * const iter = range(1, 10); * * for (const [err, val] of _try(iter)) { * if (err) { * console.error("something went wrong:", err); * } else { * console.log("current value:", val); * } * } * ``` */ export default function _try<E = unknown, T = any, TReturn = any, TNext = unknown>(gen: Generator<T, TReturn, TNext>): Generator<[E, T], [E, TReturn], TNext>; /** * Resolves a promise and renders its result in an `[err, res]` tuple. * * @example * ```ts * import _try from "@ayonli/jsext/try"; * import axios from "axios"; * * let [err, res] = await _try(axios.get("https://example.org")); * * if (err) { * res = (err as any)["response"]; * } * ``` */ export default function _try<E = unknown, R = any>(job: PromiseLike<R>): Promise<[E, R]>;