From 5a96702a5bc06bd17866815b6664293eade51225 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Fri, 7 Feb 2020 11:40:00 +0100 Subject: [PATCH 1/3] =?UTF-8?q?util:=20Use=C2=A0a=C2=A0global=C2=A0symbol?= =?UTF-8?q?=20for=C2=A0`util.promisify.custom`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/issues/31647 --- lib/internal/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/util.js b/lib/internal/util.js index 5eb5ba9e966ff9..f26ea970e8dce0 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -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) { From 17982663cb7c8069e77b0ac65f331aec8d9fe3d3 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Fri, 7 Feb 2020 12:00:00 +0100 Subject: [PATCH 2/3] =?UTF-8?q?docs(util):=20Use=C2=A0a=C2=A0global=C2=A0s?= =?UTF-8?q?ymbol=20for=C2=A0`util.promisify.custom`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/api/util.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/api/util.md b/doc/api/util.md index 9f48a5b8fd72e0..5585279d2bb45c 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -995,11 +995,32 @@ throw an error. ### `util.promisify.custom` * {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`