Skip to content

Commit

Permalink
doc update fs.read() documentation for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Mert Can Altin committed Apr 11, 2024
1 parent df9cb97 commit ab76d28
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,18 @@ extended part is filled with null bytes (`'\0'`):
If `len` is negative then `0` will be used.
The `fs.read()` method reads data from the file specified by the file descriptor (`fd`). The `length` argument indicates the maximum number of bytes that Node.js will attempt to read from the kernel. However, the actual number of bytes read (`bytesRead`) can be lower than the specified `length` for various reasons.
For example:
- If the file is shorter than the specified `length`, `bytesRead` will be set to the actual number of bytes read.
- If the file encounters EOF (End of File) before the buffer could be filled, the callback will be called with an error.
- If the file is longer than the specified `length`, only `length` bytes will be read from the file, and `bytesRead` will be set to `length`.
- If the file is on a slow network filesystem or encounters any other issue during reading, `bytesRead` can be lower than the specified `length`.
Therefore, when using `fs.read()`, it's important to check the `bytesRead` value to determine how many bytes were actually read from the file. Depending on your application logic, you may need to handle cases where `bytesRead` is lower than the specified `length`, such as by wrapping the read call in a loop if you require a minimum amount of bytes.
This behavior is similar to the POSIX `preadv2` function.
### `fs.futimes(fd, atime, mtime, callback)`
<!-- YAML
Expand Down

0 comments on commit ab76d28

Please sign in to comment.