Promisify
[<Interface>]
type Promisify =
abstract member custom: Symbol
abstract member Invoke (fn: Function) : Function
abstract member Invoke<'T1, 'T2, 'T3, 'T4, 'T5> (fn: Fn10<'T1,'T2,'T3,'T4,'T5>) : Result8<'T1,'T2,'T3,'T4,'T5>
abstract member Invoke<'T1, 'T2, 'T3, 'T4, 'T5, 'TResult> (fn: Fn9<'T1,'T2,'T3,'T4,'T5,'TResult>) : Result7<'T1,'T2,'T3,'T4,'T5,'TResult>
abstract member Invoke<'T1, 'T2, 'T3, 'T4> (fn: Fn8<'T1,'T2,'T3,'T4>) : Result6<'T1,'T2,'T3,'T4>
abstract member Invoke<'T1, 'T2, 'T3, 'T4, 'TResult> (fn: Fn7<'T1,'T2,'T3,'T4,'TResult>) : Result5<'T1,'T2,'T3,'T4,'TResult>
abstract member Invoke<'T1, 'T2, 'T3> (fn: Fn6<'T1,'T2,'T3>) : Result4<'T1,'T2,'T3>
abstract member Invoke<'T1, 'T2, 'T3, 'TResult> (fn: Fn5<'T1,'T2,'T3,'TResult>) : Result3<'T1,'T2,'T3,'TResult>
abstract member Invoke<'T1, 'T2> (fn: Fn4<'T1,'T2>) : Result2<'T1,'T2>
abstract member Invoke<'T1, 'T2, 'TResult> (fn: Fn3<'T1,'T2,'TResult>) : Result<'T1,'T2,'TResult>
abstract member Invoke<'T1> (fn: Fn2<'T1>) : 'T1 -> Promise<unit>
abstract member Invoke<'T1, 'TResult> (fn: Fn<'T1,'TResult>) : 'T1 -> Promise<'TResult>
abstract member Invoke (fn: (obj option -> unit) -> unit) : unit -> Promise<unit>
abstract member Invoke<'TResult> (fn: Callback<'TResult> -> unit) : unit -> Promise<'TResult>
abstract member Invoke<'TCustom> (fn: U2<CustomPromisifyLegacy<'TCustom>,CustomPromisifySymbol<'TCustom>>) : 'TCustom| Assembly | Xantham.Fable.Node |
Declared inside
Fn | |
Fn10 | |
Fn2 | |
Fn3 | |
Fn4 | |
Fn5 | |
Fn6 | |
Fn7 | |
Fn8 | |
Fn9 | |
Result | |
Result2 | |
Result3 | |
Result4 | |
Result5 | |
Result6 | |
Result7 | |
Result8 |
Properties
Methods
abstract member Invoke (fn: Function) : Function
abstract member Invoke (fn: Function) : FunctionTakes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1, 'T2, 'T3, 'T4, 'T5> (fn: Fn10<'T1,'T2,'T3,'T4,'T5>) : Result8<'T1,'T2,'T3,'T4,'T5>
abstract member Invoke<'T1, 'T2, 'T3, 'T4, 'T5> (fn: Fn10<'T1,'T2,'T3,'T4,'T5>) : Result8<'T1,'T2,'T3,'T4,'T5>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1, 'T2, 'T3, 'T4, 'T5, 'TResult> (fn: Fn9<'T1,'T2,'T3,'T4,'T5,'TResult>) : Result7<'T1,'T2,'T3,'T4,'T5,'TResult>
abstract member Invoke<'T1, 'T2, 'T3, 'T4, 'T5, 'TResult> (fn: Fn9<'T1,'T2,'T3,'T4,'T5,'TResult>) : Result7<'T1,'T2,'T3,'T4,'T5,'TResult>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1, 'T2, 'T3, 'T4> (fn: Fn8<'T1,'T2,'T3,'T4>) : Result6<'T1,'T2,'T3,'T4>
abstract member Invoke<'T1, 'T2, 'T3, 'T4> (fn: Fn8<'T1,'T2,'T3,'T4>) : Result6<'T1,'T2,'T3,'T4>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1, 'T2, 'T3, 'T4, 'TResult> (fn: Fn7<'T1,'T2,'T3,'T4,'TResult>) : Result5<'T1,'T2,'T3,'T4,'TResult>
abstract member Invoke<'T1, 'T2, 'T3, 'T4, 'TResult> (fn: Fn7<'T1,'T2,'T3,'T4,'TResult>) : Result5<'T1,'T2,'T3,'T4,'TResult>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1, 'T2, 'T3> (fn: Fn6<'T1,'T2,'T3>) : Result4<'T1,'T2,'T3>
abstract member Invoke<'T1, 'T2, 'T3> (fn: Fn6<'T1,'T2,'T3>) : Result4<'T1,'T2,'T3>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1, 'T2, 'T3, 'TResult> (fn: Fn5<'T1,'T2,'T3,'TResult>) : Result3<'T1,'T2,'T3,'TResult>
abstract member Invoke<'T1, 'T2, 'T3, 'TResult> (fn: Fn5<'T1,'T2,'T3,'TResult>) : Result3<'T1,'T2,'T3,'TResult>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1, 'T2> (fn: Fn4<'T1,'T2>) : Result2<'T1,'T2>
abstract member Invoke<'T1, 'T2> (fn: Fn4<'T1,'T2>) : Result2<'T1,'T2>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1, 'T2, 'TResult> (fn: Fn3<'T1,'T2,'TResult>) : Result<'T1,'T2,'TResult>
abstract member Invoke<'T1, 'T2, 'TResult> (fn: Fn3<'T1,'T2,'TResult>) : Result<'T1,'T2,'TResult>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1> (fn: Fn2<'T1>) : 'T1 -> Promise<unit>
abstract member Invoke<'T1> (fn: Fn2<'T1>) : 'T1 -> Promise<unit>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'T1, 'TResult> (fn: Fn<'T1,'TResult>) : 'T1 -> Promise<'TResult>
abstract member Invoke<'T1, 'TResult> (fn: Fn<'T1,'TResult>) : 'T1 -> Promise<'TResult>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke (fn: (obj option -> unit) -> unit) : unit -> Promise<unit>
abstract member Invoke (fn: (obj option -> unit) -> unit) : unit -> Promise<unit>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'TResult> (fn: Callback<'TResult> -> unit) : unit -> Promise<'TResult>
abstract member Invoke<'TResult> (fn: Callback<'TResult> -> unit) : unit -> Promise<'TResult>Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0
abstract member Invoke<'TCustom> (fn: U2<CustomPromisifyLegacy<'TCustom>,CustomPromisifySymbol<'TCustom>>) : 'TCustom
abstract member Invoke<'TCustom> (fn: U2<CustomPromisifyLegacy<'TCustom>,CustomPromisifySymbol<'TCustom>>) : 'TCustomTakes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
promisifiedStat('.').then((stats) => {
// Do something with `stats`
}).catch((error) => {
// Handle the error.
});
Or, equivalently using async functions:
import { promisify } from 'node:util';
import { stat } from 'node:fs';
const promisifiedStat = promisify(stat);
async function callStat() {
const stats = await promisifiedStat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
callStat();
If there is an original[util.promisify.custom] property present, promisify will return its value, see Custom promisified functions.
promisify() assumes that original is a function taking a callback as its final argument in all cases. If original is not a function, promisify() will throw an error. If original is a function but its last argument is not an error-first callback, it will still be passed an error-first callback as its last argument.
Using promisify() on class methods or other methods that use this may not work as expected unless handled specially:
import { promisify } from 'node:util';
class Foo {
constructor() {
this.a = 42;
}
bar(callback) {
callback(null, this.a);
}
}
const foo = new Foo();
const naiveBar = promisify(foo.bar);
// TypeError: Cannot read properties of undefined (reading 'a')
// naiveBar().then(a => console.log(a));
naiveBar.call(foo).then((a) => console.log(a)); // '42'
const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'
@since v8.0.0