Skip to content

Commit

Permalink
doc: add fsPromises.readFile() example
Browse files Browse the repository at this point in the history
Signed-off-by: Tierney Cyren <hello@bnb.im>
  • Loading branch information
bnb committed Nov 15, 2021
1 parent 92ecd9f commit a580c73
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions doc/api/fs.md
Expand Up @@ -1147,6 +1147,32 @@ platform-specific. On macOS, Linux, and Windows, the promise will be rejected
with an error. On FreeBSD, a representation of the directory's contents will be
returned.
An example of reading a `package.json` file located in the same directory of the running code:
```mjs
import { readFile } from 'fs/promises';
try {
const contents = await readFile(new URL('./package.json', import.meta.url), { encoding: 'utf8' });
console.log(contents);
} catch (err) {
console.error(err.message);
}
```
```cjs
const { readFile } = require('fs/promises');
const { resolve } = require('path');
async function logFile() {
try {
const contents = await readFile(resolve('./package.json'), { encoding: 'utf8' });
console.log(contents);
} catch (err) {
console.error(err.message);
}
}
logFile();
```
It is possible to abort an ongoing `readFile` using an {AbortSignal}. If a
request is aborted the promise returned is rejected with an `AbortError`:
Expand Down

0 comments on commit a580c73

Please sign in to comment.