asyncify-helpers.d.ts 1.4 KB

123456789101112131415161718192021222324
  1. declare function awaitYield<T>(value: T | Promise<T>): Generator<T | Promise<T>, T, T>;
  2. declare function awaitYieldOf<T, Yielded>(generator: Generator<Yielded | Promise<Yielded>, T, Yielded>): Generator<T | Promise<T>, T, T>;
  3. export type AwaitYield = typeof awaitYield & {
  4. of: typeof awaitYieldOf;
  5. };
  6. /**
  7. * Create a function that may or may not be async, using a generator
  8. *
  9. * Within the generator, call `yield* awaited(maybePromise)` to await a value
  10. * that may or may not be a promise.
  11. *
  12. * If the inner function never yields a promise, it will return synchronously.
  13. */
  14. export declare function maybeAsyncFn<
  15. /** Function arguments */
  16. Args extends any[], This,
  17. /** Function return type */
  18. Return,
  19. /** Yields to unwrap */
  20. Yielded>(that: This, fn: (this: This, awaited: AwaitYield, ...args: Args) => Generator<Yielded | Promise<Yielded>, Return, Yielded>): (...args: Args) => Return | Promise<Return>;
  21. export type MaybeAsyncBlock<Return, This, Yielded, Args extends any[] = []> = (this: This, awaited: AwaitYield, ...args: Args) => Generator<Yielded | Promise<Yielded>, Return, Yielded>;
  22. export declare function maybeAsync<Return, This, Yielded>(that: This, startGenerator: (this: This, await: AwaitYield) => Generator<Yielded | Promise<Yielded>, Return, Yielded>): Return | Promise<Return>;
  23. export declare function awaitEachYieldedPromise<Yielded, Returned>(gen: Generator<Yielded | Promise<Yielded>, Returned, Yielded>): Returned | Promise<Returned>;
  24. export {};