Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: define custom promisify implementations #45

Merged
merged 2 commits into from
Nov 10, 2022
Merged

Conversation

rwe
Copy link
Contributor

@rwe rwe commented Nov 4, 2022

This defines properties on each of the exported asynchronous functions which allow them to work with Node's promisify.

The existing callbacks were incompatible by default, because they:

  • are not given as the last arguments of those functions
  • do not accept optional "error" as their first arguments

In other words, they are not "Node-style" callbacks.

This implementation chooses to reject promises rather than returning undefined on those errors as the callbacks do: presumably the undefined value is used simply because the existing callback format forgot to allow for an error parameter.

This defines both the modern promisify.custom symbol and the "old" __promisify__ property, since the former isn't actually describable in TypeScript definitions yet, and the latter is still used in Node's definition files (e.g. for child_process functions). See:

Example usage:

import { promisify } from 'util';
import * as winProcTree from 'windows-process-tree';

const getProcessTree = promisify(winProcTree.getProcessTree);

async function doThings() {
  const myProcesses = await getProcessTree(process.pid);
  // etc.
}

@rwe
Copy link
Contributor Author

rwe commented Nov 4, 2022

@microsoft-github-policy-service agree

@Tyriar Tyriar self-assigned this Nov 9, 2022
@rwe rwe marked this pull request as draft November 10, 2022 00:14
rwe added 2 commits November 9, 2022 18:49
This defines properties on each of the exported asynchronous functions
which allow them to work with Node's `promisify`.

The existing callbacks were incompatible by default, because they:
  • are not given as the last arguments of those functions
  • do not accept optional "error" as their first arguments

In other words, they are not "Node-style".

This implementation chooses to reject promises rather than returning
`undefined` on those errors, as the callbacks do: presumably the
`undefined` value is used simply because the existing callback format
forgot to allow for an error parameter.

This defines both the modern `promisify.custom` symbol and the "old"
`__promisify__` property, since the former isn't actually describable in
TypeScript definitions yet, and the latter is still used in Node's
definition files (e.g. for `child_process` functions). See:
  • microsoft/TypeScript#36813DefinitelyTyped/DefinitelyTyped#42154
This adds the module 'windows-process-tree/promises', exposing the
promisified variants of the primary library methods.

It would be nice to also make this a property on the
'windows-process-tree' index module, but with the current structure that
requires some work to avoid a mutually-recursive import.

Since the `index.ts` file sets the [promisify.custom]/__promisify__
properties at the top level, and the './promises' module uses those at
the top level to expose the promisified implementations, there's an
initialization mutual dependency.

The simplest thing, done here, is to only support explicit importing of
`windows-process-tree/promises`.

To do it properly, either:
 • one of `index.ts` or `promises.ts` exposes lazy-init'd properties
   via a `defineProperty` getter on `exports`
 • or, simplest but noisiest, move the current `index.ts` to
   something like `_core.ts`, and create an `index.ts` with:
     export * from './_core';
     export * as promises from './promises';

However that doesn't don't seem worth immediate testing effort, and can
always be done later without breaking API compatibility.
@rwe rwe marked this pull request as ready for review November 10, 2022 03:01
@rwe rwe requested a review from Tyriar November 10, 2022 03:01
@Tyriar Tyriar requested a review from rzhao271 November 10, 2022 15:58
@Tyriar Tyriar merged commit a083648 into microsoft:main Nov 10, 2022
@rwe rwe deleted the promisify branch November 11, 2022 06:24
@Tyriar Tyriar added this to the 0.4.0 milestone Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants