| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a44219d commit e7f3f0d
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -984,12 +984,15 @@ import { readFile } from 'fs/promises'; | |||
| 984 | 984 | ||
| 985 | 985 | try { | |
| 986 | 986 | const controller = new AbortController(); | |
| 987 | - const signal = controller.signal; | ||
| 988 | - readFile(fileName, { signal }); | ||
| 987 | + const { signal } = controller; | ||
| 988 | + const promise = readFile(fileName, { signal }); | ||
| 989 | 989 | ||
| 990 | - // Abort the request | ||
| 990 | + // Abort the request before the promise settles. | ||
| 991 | 991 | controller.abort(); | |
| 992 | + | ||
| 993 | + await promise; | ||
| 992 | 994 | } catch (err) { | |
| 995 | + // When a request is aborted - err is an AbortError | ||
| 993 | 996 | console.error(err); | |
| 994 | 997 | } | |
| 995 | 998 | ``` | |
@@ -1003,7 +1006,6 @@ Any specified {FileHandle} has to support reading. | |||
| 1003 | 1006 | <!-- YAML | |
| 1004 | 1007 | added: v10.0.0 | |
| 1005 | 1008 | --> | |
| 1006 | - | ||
| 1007 | 1009 | * `path` {string|Buffer|URL} | |
| 1008 | 1010 | * `options` {string|Object} | |
| 1009 | 1011 | * `encoding` {string} **Default:** `'utf8'` | |
@@ -1309,8 +1311,12 @@ try { | |||
| 1309 | 1311 | const controller = new AbortController(); | |
| 1310 | 1312 | const { signal } = controller; | |
| 1311 | 1313 | const data = new Uint8Array(Buffer.from('Hello Node.js')); | |
| 1312 | - writeFile('message.txt', data, { signal }); | ||
| 1314 | + const promise = writeFile('message.txt', data, { signal }); | ||
| 1315 | + | ||
| 1316 | + // Abort the request before the promise settles. | ||
| 1313 | 1317 | controller.abort(); | |
| 1318 | + | ||
| 1319 | + await promise; | ||
| 1314 | 1320 | } catch (err) { | |
| 1315 | 1321 | // When a request is aborted - err is an AbortError | |
| 1316 | 1322 | console.error(err); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments