From a580c737e69a669c59f9f2421791d9650b88b29b Mon Sep 17 00:00:00 2001 From: Tierney Cyren Date: Mon, 15 Nov 2021 17:22:46 -0500 Subject: [PATCH 1/7] doc: add fsPromises.readFile() example Signed-off-by: Tierney Cyren --- doc/api/fs.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index 3facd3b4b0586a..1c7adc33bed2dc 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -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`: From 4805e1cc4dc1f97bdc1e834b6459999dc3e6ebc8 Mon Sep 17 00:00:00 2001 From: Tierney Cyren Date: Mon, 10 Oct 2022 19:03:27 +0200 Subject: [PATCH 2/7] doc: simplify code example Co-authored-by: Rich Trott --- doc/api/fs.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 1c7adc33bed2dc..1472f7b7c1f43b 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1151,7 +1151,8 @@ An example of reading a `package.json` file located in the same directory of the ```mjs import { readFile } from 'fs/promises'; try { - const contents = await readFile(new URL('./package.json', import.meta.url), { encoding: 'utf8' }); + const filePath = new URL('./package.json', import.meta.url); + const contents = await readFile(filePath, { encoding: 'utf8' }); console.log(contents); } catch (err) { From 88b8b0847f44ca7f4fbfe0eca3294e5c3f2877e9 Mon Sep 17 00:00:00 2001 From: Tierney Cyren Date: Mon, 10 Oct 2022 19:03:44 +0200 Subject: [PATCH 3/7] doc: simplify code example Co-authored-by: Rich Trott --- doc/api/fs.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 1472f7b7c1f43b..659828edc7ddfc 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1165,7 +1165,8 @@ const { readFile } = require('fs/promises'); const { resolve } = require('path'); async function logFile() { try { - const contents = await readFile(resolve('./package.json'), { encoding: 'utf8' }); + const filePath = resolve('./package.json'); + const contents = await readFile(filePath, { encoding: 'utf8' }); console.log(contents); } catch (err) { console.error(err.message); From 9dfe016dcaaa6933cd1b10587e295e45a46299bc Mon Sep 17 00:00:00 2001 From: Tierney Cyren Date: Mon, 10 Oct 2022 19:03:59 +0200 Subject: [PATCH 4/7] doc: remove unnecessary line break Co-authored-by: Rich Trott --- doc/api/fs.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 659828edc7ddfc..b167b5c6d08e9b 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1153,7 +1153,6 @@ import { readFile } from 'fs/promises'; try { const filePath = new URL('./package.json', import.meta.url); const contents = await readFile(filePath, { encoding: 'utf8' }); - console.log(contents); } catch (err) { console.error(err.message); From 410da558e24599835f8fbedf1bfe337cc6371039 Mon Sep 17 00:00:00 2001 From: Tierney Cyren Date: Tue, 11 Oct 2022 00:03:02 +0200 Subject: [PATCH 5/7] doc: fix line length issue Co-authored-by: Antoine du Hamel --- doc/api/fs.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index b167b5c6d08e9b..433ba127e48c61 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1147,7 +1147,9 @@ 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: +An example of reading a `package.json` file located in the same directory of the +running code: + ```mjs import { readFile } from 'fs/promises'; try { From bad05dddd30cf4f467762090a4e518cd4bee118c Mon Sep 17 00:00:00 2001 From: Tierney Cyren Date: Tue, 11 Oct 2022 00:03:25 +0200 Subject: [PATCH 6/7] doc: use `node:` prefix for node modules Co-authored-by: Antoine du Hamel --- doc/api/fs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 433ba127e48c61..04ce32dd9d0acc 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1151,7 +1151,7 @@ An example of reading a `package.json` file located in the same directory of the running code: ```mjs -import { readFile } from 'fs/promises'; +import { readFile } from 'node:fs/promises'; try { const filePath = new URL('./package.json', import.meta.url); const contents = await readFile(filePath, { encoding: 'utf8' }); From 90a77178015abde90b6dd9cfb6908bad936d2bcb Mon Sep 17 00:00:00 2001 From: Tierney Cyren Date: Tue, 11 Oct 2022 00:03:35 +0200 Subject: [PATCH 7/7] doc: use `node:` prefix for node modules Co-authored-by: Antoine du Hamel --- doc/api/fs.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 04ce32dd9d0acc..d6465fbfec7801 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1162,8 +1162,8 @@ try { ``` ```cjs -const { readFile } = require('fs/promises'); -const { resolve } = require('path'); +const { readFile } = require('node:fs/promises'); +const { resolve } = require('node:path'); async function logFile() { try { const filePath = resolve('./package.json');