Skip to content

Commit

Permalink
util: use a global symbol for util.promisify.custom
Browse files Browse the repository at this point in the history
Define `util.promisify.custom`
as `Symbol.for("nodejs.util.inspect.custom")`, rather than
as `Symbol("util.inspect.custom")`.

This allows custom `promisify` wrappers to easily/safely be defined
in non‑Node.js environments.

Fixes: #31647

PR-URL: #31672
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
ExE-Boss authored and MylesBorins committed Mar 24, 2020
1 parent 2069c4e commit 53fd0d8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
21 changes: 21 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,11 +995,32 @@ throw an error.
### `util.promisify.custom`
<!-- YAML
added: v8.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/31672
description: This is now defined as a shared symbol.
-->

* {symbol} that can be used to declare custom promisified variants of functions,
see [Custom promisified functions][].

In addition to being accessible through `util.promisify.custom`, this
symbol is [registered globally][global symbol registry] and can be
accessed in any environment as `Symbol.for('nodejs.util.promisify.custom')`.

For example, with a function that takes in
`(foo, onSuccessCallback, onErrorCallback)`:

```js
const kCustomPromisifiedSymbol = Symbol.for('nodejs.util.promisify.custom');

doSomething[kCustomPromisifiedSymbol] = (foo) => {
return new Promise((resolve, reject) => {
doSomething(foo, resolve, reject);
});
};
```

## Class: `util.TextDecoder`
<!-- YAML
added: v8.3.0
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function getSystemErrorName(err) {
return entry ? entry[0] : `Unknown system error ${err}`;
}

const kCustomPromisifiedSymbol = Symbol('util.promisify.custom');
const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom');
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');

function promisify(original) {
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-util-promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ const stat = promisify(fs.stat);
assert.strictEqual(promisify(promisify(fn)), promisifedFn);
}

{
function fn() {}

function promisifiedFn() {}

// util.promisify.custom is a shared symbol which can be accessed
// as `Symbol.for("nodejs.util.promisify.custom")`.
const kCustomPromisifiedSymbol = Symbol.for('nodejs.util.promisify.custom');
fn[kCustomPromisifiedSymbol] = promisifiedFn;

assert.strictEqual(kCustomPromisifiedSymbol, promisify.custom);
assert.strictEqual(promisify(fn), promisifiedFn);
assert.strictEqual(promisify(promisify(fn)), promisifiedFn);
}

{
function fn() {}
fn[promisify.custom] = 42;
Expand Down

0 comments on commit 53fd0d8

Please sign in to comment.