From a0e88eac3bc72fc0bbb1078b1cc1355e6f7bc8d1 Mon Sep 17 00:00:00 2001 From: Moritz Kneilmann Date: Thu, 13 May 2021 15:26:26 +0200 Subject: [PATCH 1/5] Update abort signal in fs promise api example Without `await`ing the promise the try-catch block would never catch this error. IMO this was misleading. --- doc/api/fs.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 4f88ce44027026..6b53f391427320 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -980,12 +980,15 @@ import { readFile } from 'fs/promises'; try { const controller = new AbortController(); - const signal = controller.signal; - readFile(fileName, { signal }); + const { signal } = controller; + const promise = readFile(fileName, { signal }); - // Abort the request + // Abort the request before the promise settled controller.abort(); + + await promise; } catch (err) { + // When a request is aborted - err is an AbortError console.error(err); } ``` @@ -1316,8 +1319,12 @@ try { const controller = new AbortController(); const { signal } = controller; const data = new Uint8Array(Buffer.from('Hello Node.js')); - writeFile('message.txt', data, { signal }); + const promise = writeFile('message.txt', data, { signal }); + + // Abort the request before the promise settled controller.abort(); + + await promise; } catch (err) { // When a request is aborted - err is an AbortError console.error(err); From 69795ffa9e0589948ec79dfb0fba20a38e46b49b Mon Sep 17 00:00:00 2001 From: Moritz Kneilmann Date: Thu, 13 May 2021 15:56:23 +0200 Subject: [PATCH 2/5] Update doc/api/fs.md 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 6b53f391427320..f741e8bf3a982c 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -983,7 +983,7 @@ try { const { signal } = controller; const promise = readFile(fileName, { signal }); - // Abort the request before the promise settled + // Abort the request before the promise settles. controller.abort(); await promise; From b5ad79c04752338891931eb9652558fc325c3c88 Mon Sep 17 00:00:00 2001 From: Moritz Kneilmann Date: Thu, 13 May 2021 15:56:27 +0200 Subject: [PATCH 3/5] Update doc/api/fs.md 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 f741e8bf3a982c..6ef7be0e823056 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1321,7 +1321,7 @@ try { const data = new Uint8Array(Buffer.from('Hello Node.js')); const promise = writeFile('message.txt', data, { signal }); - // Abort the request before the promise settled + // Abort the request before the promise settles. controller.abort(); await promise; From 584a22755d832b7339e778d821cbbc8d7454ef61 Mon Sep 17 00:00:00 2001 From: Moritz Kneilmann Date: Thu, 13 May 2021 16:06:40 +0200 Subject: [PATCH 4/5] Remove trailing spaces --- doc/api/fs.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 6ef7be0e823056..ac902c53291f71 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -985,7 +985,7 @@ try { // Abort the request before the promise settles. controller.abort(); - + await promise; } catch (err) { // When a request is aborted - err is an AbortError @@ -1002,7 +1002,7 @@ Any specified {FileHandle} has to support reading. - + * `path` {string|Buffer|URL} * `options` {string|Object} * `encoding` {string} **Default:** `'utf8'` @@ -1323,7 +1323,7 @@ try { // Abort the request before the promise settles. controller.abort(); - + await promise; } catch (err) { // When a request is aborted - err is an AbortError From 682a00e230bc5102babd30c6b85da0fc4ec95e52 Mon Sep 17 00:00:00 2001 From: Moritz Kneilmann Date: Fri, 14 May 2021 00:47:21 +0200 Subject: [PATCH 5/5] Update doc/api/fs.md Co-authored-by: Antoine du Hamel --- doc/api/fs.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index ac902c53291f71..66466bad6cfaad 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1002,7 +1002,6 @@ Any specified {FileHandle} has to support reading. - * `path` {string|Buffer|URL} * `options` {string|Object} * `encoding` {string} **Default:** `'utf8'`