Skip to content

Commit b280c86

Browse files
lholmquistcodebytere
authored andcommitted
fs: support util.promisify for fs.readv
PR-URL: #33590 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent cb8b9ec commit b280c86

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/api/fs.md

+3
Original file line numberDiff line numberDiff line change
@@ -3191,6 +3191,9 @@ from the current position.
31913191
The callback will be given three arguments: `err`, `bytesRead`, and
31923192
`buffers`. `bytesRead` is how many bytes were read from the file.
31933193

3194+
If this method is invoked as its [`util.promisify()`][]ed version, it returns
3195+
a `Promise` for an `Object` with `bytesRead` and `buffers` properties.
3196+
31943197
## `fs.readvSync(fd, buffers[, position])`
31953198
<!-- YAML
31963199
added: v14.0.0

lib/fs.js

+3
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ function readv(fd, buffers, position, callback) {
597597
return binding.readBuffers(fd, buffers, position, req);
598598
}
599599

600+
ObjectDefineProperty(readv, internalUtil.customPromisifyArgs,
601+
{ value: ['bytesRead', 'buffers'], enumerable: false });
602+
600603
function readvSync(fd, buffers, position) {
601604
validateInt32(fd, 'fd', 0);
602605
validateBufferArray(buffers);
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const fs = require('fs');
6+
const readv = require('util').promisify(fs.readv);
7+
const assert = require('assert');
8+
const filepath = fixtures.path('x.txt');
9+
const fd = fs.openSync(filepath, 'r');
10+
11+
const expected = [Buffer.from('xyz\n')];
12+
13+
readv(fd, expected)
14+
.then(function({ bytesRead, buffers }) {
15+
assert.deepStrictEqual(bytesRead, expected[0].length);
16+
assert.deepStrictEqual(buffers, expected);
17+
})
18+
.then(common.mustCall());

0 commit comments

Comments
 (0)