@@ -39,15 +39,21 @@ execute **asynchronously**.
3939
4040Using the File System module as an example, this is a ** synchronous** file read:
4141
42- ``` js
42+ ``` cjs
4343const fs = require (' node:fs' );
4444
4545const data = fs .readFileSync (' /file.md' ); // blocks here until file is read
4646```
4747
48+ ``` mjs
49+ import fs from ' node:fs' ;
50+
51+ const data = fs .readFileSync (' /file.md' ); // blocks here until file is read
52+ ```
53+
4854And here is an equivalent ** asynchronous** example:
4955
50- ``` js
56+ ``` cjs
5157const fs = require (' node:fs' );
5258
5359fs .readFile (' /file.md' , (err , data ) => {
@@ -57,6 +63,16 @@ fs.readFile('/file.md', (err, data) => {
5763});
5864```
5965
66+ ``` mjs
67+ import fs from ' node:fs' ;
68+
69+ fs .readFile (' /file.md' , (err , data ) => {
70+ if (err) {
71+ throw err;
72+ }
73+ });
74+ ```
75+
6076The first example appears simpler than the second but has the disadvantage of
6177the second line ** blocking** the execution of any additional JavaScript until
6278the entire file is read. Note that in the synchronous version if an error is
@@ -66,17 +82,25 @@ shown.
6682
6783Let's expand our example a little bit:
6884
69- ``` js
85+ ``` cjs
7086const fs = require (' node:fs' );
7187
7288const data = fs .readFileSync (' /file.md' ); // blocks here until file is read
7389console .log (data);
7490moreWork (); // will run after console.log
7591```
7692
93+ ``` mjs
94+ import fs from ' node:fs' ;
95+
96+ const data = fs .readFileSync (' /file.md' ); // blocks here until file is read
97+ console .log (data);
98+ moreWork (); // will run after console.log
99+ ```
100+
77101And here is a similar, but not equivalent asynchronous example:
78102
79- ``` js
103+ ``` cjs
80104const fs = require (' node:fs' );
81105
82106fs .readFile (' /file.md' , (err , data ) => {
@@ -89,6 +113,19 @@ fs.readFile('/file.md', (err, data) => {
89113moreWork (); // will run before console.log
90114```
91115
116+ ``` mjs
117+ import fs from ' node:fs' ;
118+
119+ fs .readFile (' /file.md' , (err , data ) => {
120+ if (err) {
121+ throw err;
122+ }
123+
124+ console .log (data);
125+ });
126+ moreWork (); // will run before console.log
127+ ```
128+
92129In the first example above, ` console.log ` will be called before ` moreWork() ` . In
93130the second example ` fs.readFile() ` is ** non-blocking** so JavaScript execution
94131can continue and ` moreWork() ` will be called first. The ability to run
@@ -118,7 +155,7 @@ threads may be created to handle concurrent work.
118155There are some patterns that should be avoided when dealing with I/O. Let's look
119156at an example:
120157
121- ``` js
158+ ``` cjs
122159const fs = require (' node:fs' );
123160
124161fs .readFile (' /file.md' , (err , data ) => {
@@ -131,12 +168,25 @@ fs.readFile('/file.md', (err, data) => {
131168fs .unlinkSync (' /file.md' );
132169```
133170
171+ ``` mjs
172+ import fs from ' node:fs' ;
173+
174+ fs .readFile (' /file.md' , (err , data ) => {
175+ if (err) {
176+ throw err;
177+ }
178+
179+ console .log (data);
180+ });
181+ fs .unlinkSync (' /file.md' );
182+ ```
183+
134184In the above example, ` fs.unlinkSync() ` is likely to be run before
135185` fs.readFile() ` , which would delete ` file.md ` before it is actually read. A
136186better way to write this, which is completely ** non-blocking** and guaranteed to
137187execute in the correct order is:
138188
139- ``` js
189+ ``` cjs
140190const fs = require (' node:fs' );
141191
142192fs .readFile (' /file.md' , (readFileErr , data ) => {
@@ -154,6 +204,24 @@ fs.readFile('/file.md', (readFileErr, data) => {
154204});
155205```
156206
207+ ``` mjs
208+ import fs from ' node:fs' ;
209+
210+ fs .readFile (' /file.md' , (readFileErr , data ) => {
211+ if (readFileErr) {
212+ throw readFileErr;
213+ }
214+
215+ console .log (data);
216+
217+ fs .unlink (' /file.md' , unlinkErr => {
218+ if (unlinkErr) {
219+ throw unlinkErr;
220+ }
221+ });
222+ });
223+ ```
224+
157225The above places a ** non-blocking** call to ` fs.unlink() ` within the callback of
158226` fs.readFile() ` which guarantees the correct order of operations.
159227
0 commit comments