UNPKG

decorio

Version:

First-class ECMAScript decorators for caching, binding, and concurrency patterns

36 lines (35 loc) 1.32 kB
type TimeoutDecorator = { <A extends Array<unknown>, R extends Promise<unknown>>(value: unknown, context: ClassMethodDecoratorContext<object, (...args: A) => R>): (...args: A) => R; <A extends Array<unknown>, R extends Promise<unknown>>(value: unknown, context: ClassFieldDecoratorContext<object, (...args: A) => R>): (originalFn: (...args: A) => R) => (...args: A) => R; }; /** * 🎯 Decorator `@timeout` enforces a maximum execution time for an async method. * If the method does not complete within the specified number of milliseconds, * it will be aborted via an `AbortSignal`. * * Decorator exposes a static property `timeout.signal` that the method can read at runtime. * * Usage: * ```typescript * class Example { * @timeout(500) async fetchData(id: string): Promise<Data> { * const { signal } = timeout; * * return fetch(`/api/data/${id}`, { signal }).then((r) => r.json()); * } * } * * const e = new Example(); * try { * const data = await e.fetchData("123"); * console.log('Got data:', data); * } catch (e) { * console.error(e.message); // If over 500 ms: "timeout 500ms exceeded" * } * ``` */ export declare function timeout(timeoutMs: number): TimeoutDecorator; export declare namespace timeout { var signal: AbortSignal | undefined; } export {};