Skip to content
This repository was archived by the owner on Sep 23, 2021. It is now read-only.

Provide namespace for promisyfing#124

Open
dalechyn wants to merge 2 commits intojorangreef:masterfrom
dalechyn:patch-1
Open

Provide namespace for promisyfing#124
dalechyn wants to merge 2 commits intojorangreef:masterfrom
dalechyn:patch-1

Conversation

@dalechyn
Copy link

@dalechyn dalechyn commented Jun 5, 2020

child_process.d.ts has next namespace for util.promisify:

// NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
namespace exec {
        function __promisify__(command: string): PromiseWithChild<{ stdout: string, stderr: string }>;
        function __promisify__(command: string, options: { encoding: "buffer" | null } & ExecOptions): PromiseWithChild<{ stdout: Buffer, stderr: Buffer }>;
        function __promisify__(command: string, options: { encoding: BufferEncoding } & ExecOptions): PromiseWithChild<{ stdout: string, stderr: string }>;
        function __promisify__(command: string, options: ExecOptions): PromiseWithChild<{ stdout: string, stderr: string }>;
        function __promisify__(command: string, options?: ({ encoding?: string | null } & ExecOptions) | null): PromiseWithChild<{ stdout: string | Buffer, stderr: string | Buffer }>;
    }

It allows wrapping error-first contracts in promises.
I've tested it on my application with having module augmentation.

declare module 'sudo-prompt' {
  // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
  namespace exec {
    function __promisify__(command: string): PromiseWithChild<{ stdout: string; stderr: string }>
    function __promisify__(
      command: string,
      options: { encoding: 'buffer' | null } & ExecOptions
    ): PromiseWithChild<{ stdout: Buffer; stderr: Buffer }>
    function __promisify__(
      command: string,
      options: { encoding: BufferEncoding } & ExecOptions
    ): PromiseWithChild<{ stdout: string; stderr: string }>
    function __promisify__(
      command: string,
      options: ExecOptions
    ): PromiseWithChild<{ stdout: string; stderr: string }>
    function __promisify__(
      command: string,
      options?: ({ encoding?: string | null } & ExecOptions) | null
    ): PromiseWithChild<{ stdout: string | Buffer; stderr: string | Buffer }>
  }
}

And it works!

Vladyslav Dalechyn added 2 commits June 5, 2020 12:41
Allows having async/await calls as child_process.exec does.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant