| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5b25fbe commit 8590c15
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -980,12 +980,15 @@ import { readFile } from 'fs/promises'; | |||
| 980 | 980 | ||
| 981 | 981 | try { | |
| 982 | 982 | const controller = new AbortController(); | |
| 983 | - const signal = controller.signal; | ||
| 984 | - readFile(fileName, { signal }); | ||
| 983 | + const { signal } = controller; | ||
| 984 | + const promise = readFile(fileName, { signal }); | ||
| 985 | 985 | ||
| 986 | - // Abort the request | ||
| 986 | + // Abort the request before the promise settles. | ||
| 987 | 987 | controller.abort(); | |
| 988 | + | ||
| 989 | + await promise; | ||
| 988 | 990 | } catch (err) { | |
| 991 | + // When a request is aborted - err is an AbortError | ||
| 989 | 992 | console.error(err); | |
| 990 | 993 | } | |
| 991 | 994 | ``` | |
@@ -999,7 +1002,6 @@ Any specified {FileHandle} has to support reading. | |||
| 999 | 1002 | <!-- YAML | |
| 1000 | 1003 | added: v10.0.0 | |
| 1001 | 1004 | --> | |
| 1002 | - | ||
| 1003 | 1005 | * `path` {string|Buffer|URL} | |
| 1004 | 1006 | * `options` {string|Object} | |
| 1005 | 1007 | * `encoding` {string} **Default:** `'utf8'` | |
@@ -1316,8 +1318,12 @@ try { | |||
| 1316 | 1318 | const controller = new AbortController(); | |
| 1317 | 1319 | const { signal } = controller; | |
| 1318 | 1320 | const data = new Uint8Array(Buffer.from('Hello Node.js')); | |
| 1319 | - writeFile('message.txt', data, { signal }); | ||
| 1321 | + const promise = writeFile('message.txt', data, { signal }); | ||
| 1322 | + | ||
| 1323 | + // Abort the request before the promise settles. | ||
| 1320 | 1324 | controller.abort(); | |
| 1325 | + | ||
| 1326 | + await promise; | ||
| 1321 | 1327 | } catch (err) { | |
| 1322 | 1328 | // When a request is aborted - err is an AbortError | |
| 1323 | 1329 | console.error(err); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments