Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 53fd0d8

Browse filesBrowse 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 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>
1 parent 2069c4e commit 53fd0d8
Copy full SHA for 53fd0d8

File tree

Expand file treeCollapse file tree

3 files changed

+37
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+37
-1
lines changed
Open diff view settings
Collapse file

‎doc/api/util.md‎

Copy file name to clipboardExpand all lines: doc/api/util.md
+21Lines changed: 21 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -995,11 +995,32 @@ throw an error.
995995
### `util.promisify.custom`
996996
<!-- YAML
997997
added: v8.0.0
998+
changes:
999+
- version: REPLACEME
1000+
pr-url: https://github.com/nodejs/node/pull/31672
1001+
description: This is now defined as a shared symbol.
9981002
-->
9991003

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

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

‎lib/internal/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/util.js
+1-1Lines 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) {
Collapse file

‎test/parallel/test-util-promisify.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-promisify.js
+15Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.