Skip to content

Commit 32c0449

Browse files
ExE-BossMylesBorins
authored andcommitted
util: use a global symbol for util.promisify.custom
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 Backport-PR-URL: #32349 PR-URL: #31672 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent eee587b commit 32c0449

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

doc/api/util.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,11 +992,32 @@ throw an error.
992992
### `util.promisify.custom`
993993
<!-- YAML
994994
added: v8.0.0
995+
changes:
996+
- version: REPLACEME
997+
pr-url: https://github.com/nodejs/node/pull/31672
998+
description: This is now defined as a shared symbol.
995999
-->
9961000

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

1004+
In addition to being accessible through `util.promisify.custom`, this
1005+
symbol is [registered globally][global symbol registry] and can be
1006+
accessed in any environment as `Symbol.for('nodejs.util.promisify.custom')`.
1007+
1008+
For example, with a function that takes in
1009+
`(foo, onSuccessCallback, onErrorCallback)`:
1010+
1011+
```js
1012+
const kCustomPromisifiedSymbol = Symbol.for('nodejs.util.promisify.custom');
1013+
1014+
doSomething[kCustomPromisifiedSymbol] = (foo) => {
1015+
return new Promise((resolve, reject) => {
1016+
doSomething(foo, resolve, reject);
1017+
});
1018+
};
1019+
```
1020+
10001021
## Class: `util.TextDecoder`
10011022
<!-- YAML
10021023
added: v8.3.0

lib/internal/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function getSystemErrorName(err) {
271271
return entry ? entry[0] : `Unknown system error ${err}`;
272272
}
273273

274-
const kCustomPromisifiedSymbol = Symbol('util.promisify.custom');
274+
const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom');
275275
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
276276

277277
function promisify(original) {

test/parallel/test-util-promisify.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ const stat = promisify(fs.stat);
3333
assert.strictEqual(promisify(promisify(fn)), promisifedFn);
3434
}
3535

36+
{
37+
function fn() {}
38+
39+
function promisifiedFn() {}
40+
41+
// util.promisify.custom is a shared symbol which can be accessed
42+
// as `Symbol.for("nodejs.util.promisify.custom")`.
43+
const kCustomPromisifiedSymbol = Symbol.for('nodejs.util.promisify.custom');
44+
fn[kCustomPromisifiedSymbol] = promisifiedFn;
45+
46+
assert.strictEqual(kCustomPromisifiedSymbol, promisify.custom);
47+
assert.strictEqual(promisify(fn), promisifiedFn);
48+
assert.strictEqual(promisify(promisify(fn)), promisifiedFn);
49+
}
50+
3651
{
3752
function fn() {}
3853
fn[promisify.custom] = 42;

0 commit comments

Comments
 (0)