From 8590c151cd918156be068db166fbd053716b396e Mon Sep 17 00:00:00 2001 From: Moritz Kneilmann Date: Thu, 13 May 2021 15:26:26 +0200 Subject: [PATCH] doc: update abort signal in fs promise api example PR-URL: https://github.com/nodejs/node/pull/38669 Reviewed-By: Antoine du Hamel Reviewed-By: Darshan Sen Reviewed-By: James M Snell Reviewed-By: Rich Trott --- doc/api/fs.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 9f7ea62ad12e42..366abb7a0735a4 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 settles. controller.abort(); + + await promise; } catch (err) { + // When a request is aborted - err is an AbortError console.error(err); } ``` @@ -999,7 +1002,6 @@ Any specified {FileHandle} has to support reading. - * `path` {string|Buffer|URL} * `options` {string|Object} * `encoding` {string} **Default:** `'utf8'` @@ -1316,8 +1318,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 settles. controller.abort(); + + await promise; } catch (err) { // When a request is aborted - err is an AbortError console.error(err);