-
Notifications
You must be signed in to change notification settings - Fork 775
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs for using promises with fs.read() & fs.write()
- Loading branch information
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# About `fs.read()` & `fs.write()` | ||
|
||
[`fs.read()`](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback) & [`fs.write()`](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback) are different from other `fs` methods in that their callbacks are called with 3 arguments instead of the usual 2 arguments. | ||
|
||
If you're using them with callbacks, they will behave as usual. However, their promise usage is a little different. `fs-extra` promisifies these methods like [`util.promisify()`](https://nodejs.org/api/util.html#util_util_promisify_original) (only availible in Node 8+) does. | ||
|
||
Here's the example promise usage: | ||
|
||
## `fs.read()` | ||
|
||
```js | ||
// Basic promises | ||
fs.read(fd, buffer, offset, length, position) | ||
.then(results => { | ||
console.log(results) | ||
// { bytesRead: 20, buffer: <Buffer 0f 34 5d ...> } | ||
}) | ||
|
||
// Async/await usage: | ||
async function example () { | ||
const { bytesRead, buffer } = await fs.read(fd, buffer, offset, length, position) | ||
} | ||
``` | ||
|
||
## `fs.write()` | ||
|
||
```js | ||
// Basic promises | ||
fs.write(fd, buffer, offset, length, position) | ||
.then(results => { | ||
console.log(results) | ||
// { bytesWritten: 20, buffer: <Buffer 0f 34 5d ...> } | ||
}) | ||
|
||
// Async/await usage: | ||
async function example () { | ||
const { bytesWritten, buffer } = await fs.write(fd, buffer, offset, length, position) | ||
} | ||
``` |