pify

  • Version 6.1.0
  • Published
  • 13.6 kB
  • No dependencies
  • MIT license

Install

npm i pify
yarn add pify
pnpm add pify

Overview

Promisify a callback-style function

Index

Functions

function pify

pify: {
<
FirstArgument,
Arguments extends readonly unknown[],
MultiArgs extends boolean = false,
ErrorFirst extends boolean = true
>(
input: (argument: FirstArgument, ...arguments_: Arguments) => any,
options?: Options<[], [], MultiArgs, ErrorFirst>
): Promisify<
[FirstArgument, ...Arguments],
InternalOptions<[], [], MultiArgs, ErrorFirst>
>;
<
Module extends Record<string, any>,
Includes extends readonly (keyof Module)[] = [],
Excludes extends readonly (keyof Module)[] = [],
MultiArgs extends boolean = false,
ErrorFirst extends boolean = true
>(
module: Module,
options?: Options<Includes, Excludes, MultiArgs, ErrorFirst, true>
): PromisifyModule<Module, MultiArgs, ErrorFirst, Includes, Excludes>;
};

    Type Aliases

    type DropLastArrayElement

    type DropLastArrayElement<T extends readonly unknown[]> = T extends [
    ...infer U,
    unknown
    ]
    ? U
    : [];

      type InternalOptions

      type InternalOptions<
      Includes extends readonly unknown[],
      Excludes extends readonly unknown[],
      MultiArgs extends boolean = false,
      ErrorFirst extends boolean = true
      > = {
      multiArgs: MultiArgs;
      include: Includes;
      exclude: Excludes;
      errorFirst: ErrorFirst;
      };

        type LastArrayElement

        type LastArrayElement<T extends readonly unknown[]> = T extends [...any, infer L]
        ? L
        : never;

          type Options

          type Options<
          Includes extends readonly unknown[],
          Excludes extends readonly unknown[],
          MultiArgs extends boolean = false,
          ErrorFirst extends boolean = true,
          ExcludeMain extends boolean = false
          > = {
          multiArgs?: MultiArgs;
          include?: Includes;
          exclude?: Excludes;
          errorFirst?: ErrorFirst;
          promiseModule?: PromiseConstructor;
          excludeMain?: ExcludeMain;
          };

            type Promisify

            type Promisify<
            Args extends readonly unknown[],
            GenericOptions extends InternalOptions<
            readonly unknown[],
            readonly unknown[],
            boolean,
            boolean
            >
            > = (...args: DropLastArrayElement<Args>) => LastArrayElement<Args> extends (
            ...arguments_: any
            ) => any
            ? // For single-argument functions when errorFirst: true we just return Promise<unknown> as it will always reject.
            Parameters<LastArrayElement<Args>> extends [infer SingleCallbackArg]
            ? GenericOptions extends { errorFirst: true }
            ? Promise<unknown>
            : Promise<SingleCallbackArg>
            : Promise<
            GenericOptions extends { multiArgs: false }
            ? LastArrayElement<Parameters<LastArrayElement<Args>>>
            : Parameters<LastArrayElement<Args>>
            >
            : // Functions without a callback will return a promise that never settles. We model this as Promise<unknown>
            Promise<unknown>;

              type PromisifyModule

              type PromisifyModule<
              Module extends Record<string, any>,
              MultiArgs extends boolean,
              ErrorFirst extends boolean,
              Includes extends ReadonlyArray<keyof Module>,
              Excludes extends ReadonlyArray<keyof Module>
              > = {
              [K in keyof Module]: Module[K] extends (...arguments_: infer Arguments) => any
              ? K extends Includes[number]
              ? Promisify<Arguments, InternalOptions<Includes, Excludes, MultiArgs>>
              : K extends Excludes[number]
              ? Module[K]
              : StringEndsWith<K, 'Sync' | 'Stream'> extends true
              ? Module[K]
              : Promisify<
              Arguments,
              InternalOptions<Includes, Excludes, MultiArgs, ErrorFirst>
              >
              : Module[K];
              };

                type StringEndsWith

                type StringEndsWith<S, X extends string> = S extends `${infer _}${X}` ? true : false;

                  Package Files (1)

                  Dependencies (0)

                  No dependencies.

                  Dev Dependencies (5)

                  Peer Dependencies (0)

                  No peer dependencies.

                  Badge

                  To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                  You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/pify.

                  • Markdown
                    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/pify)
                  • HTML
                    <a href="https://www.jsdocs.io/package/pify"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>