Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3fd4cc9

Browse files
committed
huge restructure
1 parent 9778ca2 commit 3fd4cc9

File tree

18 files changed

+123
-140
lines changed

18 files changed

+123
-140
lines changed

‎README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ as well as based off of the following YouTube videos:
230230

231231
Check out https://github.com/Ziang-Lu/JavaScript-Learning-Notes/tree/master/ajax/2-jQuery-ajax-callback
232232

233-
3. `fetch()-API`, through `Promise`
233+
3. `Promise`-based, `fetch()-API`
234234

235-
Check out https://github.com/Ziang-Lu/JavaScript-Learning-Notes/tree/master/ajax/3-fetch-promise
235+
Check out https://github.com/Ziang-Lu/JavaScript-Learning-Notes/tree/master/ajax/3-promise-fetch
236236

237-
4.`fetch()-API`, through `async/await` syntax
237+
Through `async/await` syntax
238238

239-
Check out https://github.com/Ziang-Lu/JavaScript-Learning-Notes/tree/master/ajax/4-fetch-async-await
239+
Check out https://github.com/Ziang-Lu/JavaScript-Learning-Notes/tree/master/ajax/4-promise-fetch-async-await
240240

241241
<br>
242242

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎node-crash-course/Node.md‎

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
Common built-in functions that are asynchronous and accept callback functions:
2323

24-
* `setTimeout(function, delay)`
24+
* `setTimeout(func, delay)`
2525

2626
* `Event` mechanism
2727

28-
Check out `async/1-callback/mylogger.js`
28+
Check out `src/async/1-callback/mylogger.js`
2929

3030
* Asynchronous requests (AJAX) with native `XMLHttpRequest` and `jQuery` `ajax()` method
3131

@@ -39,40 +39,40 @@
3939

4040
=> <u>Chaining a lot of callback functions => "Callback hell (回调地狱)"</u>
4141

42-
Check out `async/1-callback/fs_demo.js` as follows:
42+
Check out `src/async/1-callback/fs_demo.js` as follows:
4343

4444
```javascript
4545
const fs = require('fs');
4646
const path = require('path');
47-
47+
4848
const testFolderName = path.join(__dirname, 'test');
4949
const filename = path.join(testFolderName, 'hello.txt');
50-
50+
5151
// In order to enture the correct execution order, we need to chain the
5252
// functions as callbacks, resulting in a long callback chain, which is referred
5353
// to as "callback hell".
54-
54+
5555
// Create folder
5656
fs.mkdir(testFolderName, null, err => {
5757
if (err) throw err;
5858
console.log('Folder created.');
59-
59+
6060
// Create file
6161
// (Equivalent to writing to file)
6262
fs.writeFile(filename, 'Hello, world!', err => {
6363
if (err) throw err;
6464
console.log('Data written.');
65-
65+
6666
// Write (Append) to file
6767
fs.appendFile(filename, ' I love Node.js!', err => {
6868
if (err) throw err;
6969
console.log('Date appended.');
70-
70+
7171
// Read file
7272
fs.readFile(filename, 'utf8', (err, data) => {
7373
if (err) throw err;
7474
console.log(`Read data: ${data}`);
75-
75+
7676
// Rename file
7777
fs.rename(
7878
filename,
@@ -92,11 +92,11 @@
9292

9393
* **`Promise` (since ES6 / ES2015)**
9494

95-
* For usage, check out `src/async/2-promise/fetch_promise.js`, which contains the following code snippet, and https://github.com/Ziang-Lu/JavaScript-Learning-Notes/tree/master/ajax/3-fetch-promise
95+
* For basic usage, check out `src/async/2-promise/fetch_demo.js`, which contains the following code snippet, and https://github.com/Ziang-Lu/JavaScript-Learning-Notes/tree/master/ajax/3-promise-fetch
9696

9797
```javascript
9898
const fetch = require('node-fetch');
99-
99+
100100
// GET request
101101
fetch('https://jsonplaceholder.typicode.com/users')
102102
.then(response => response.json())
@@ -117,19 +117,15 @@
117117

118118
```javascript
119119
const fs = require('fs');
120-
120+
121121
const somePath = 'test.txt';
122-
123-
function myExistHandler() {
124-
console.log('File exists');
125-
}
126-
127-
function myNotExistHandler(err) {
128-
console.error(err);
129-
}
130-
122+
123+
const myExistHandler = () => console.log('File exists');
124+
125+
const myNotExistHandler = err => console.error(err);
126+
131127
// Originally, with callback functions, we would use "fs.exists()" like this:
132-
128+
133129
function checkExistWithCallback(path, existHandler, notExistHandler) {
134130
fs.exists(path, exists => {
135131
if (exists) {
@@ -139,19 +135,19 @@
139135
}
140136
});
141137
}
142-
138+
143139
checkExistWithCallback(somePath, myExistHandler, myNotExistHandler);
144-
140+
145141
// In order to avoid "callback hell", we want to do the same thing with Promise
146142
// like this.
147-
143+
148144
// checkExistsWithPromise(somePath)
149145
// .then(myExistHandler)
150146
// .catch(myNotExistHandler);
151-
147+
152148
// If we want to the above, we must let "checkExistsWithPromise()" return a
153149
// Promise (in which we do the necessary work).
154-
150+
155151
function checkExistsWithPromise(path) {
156152
return new Promise((resolve, reject) => {
157153
fs.exists(path, exists => {
@@ -163,20 +159,20 @@
163159
});
164160
});
165161
}
166-
162+
167163
checkExistsWithPromise(somePath)
168164
.then(myExistHandler)
169165
.catch(myNotExistHandler);
170166
```
171167

172-
* For advantage usage of Promises, including <u>sequential execution (chaining) of multiple Promises</u>, or <u>parallel execution</u>, check out `async/2-promise/promise_advanced.js` as follows:
168+
* For advantage usage of Promises, including <u>sequential execution (chaining) of multiple Promises</u>, or <u>parallel execution</u>, check out `src/async/2-promise/promise_advanced.js` as follows:
173169

174170
```javascript
175171
const { cleanRoom, removeGarbage, winIceCream } = require('../common');
176-
172+
177173
console.log('With asynchronous programming, this line is printed first.');
178-
179-
// Sequential execution (chaining)
174+
175+
// ===== Sequential execution (chaining) =====
180176
cleanRoom()
181177
.then(msg => {
182178
console.log(msg);
@@ -191,8 +187,8 @@
191187
console.log('All finished');
192188
})
193189
.catch(err => console.log(err));
194-
195-
// Parallel execution
190+
191+
// ===== Parallel execution =====
196192
Promise.all([cleanRoom(), removeGarbage(), winIceCream()])
197193
.then(values => {
198194
console.log(values);
@@ -224,23 +220,22 @@
224220

225221
***
226222

227-
这个syntax sugar是我们对于error handling, 不必再使用`.catch()`方法, 而是可以恢复到传统的synchronous programming的`try/catch`.
223+
这个syntax sugar使我们对于error handling, 不必再使用`.catch()`方法, 而是可以恢复到传统的synchronous programming的`try/catch`.
228224

229-
* For usage, check out `src/async/3-async-await/fetch_async_await.js`, which contains the following code snippet, and https://github.com/Ziang-Lu/JavaScript-Learning-Notes/tree/master/ajax/4-fetch-async-await
225+
* For usage, check out `src/async/3-promise-async-await/fetch_async_await_demo.js`, which contains the following code snippet, and https://github.com/Ziang-Lu/JavaScript-Learning-Notes/tree/master/ajax/4-promise-fetch-async-await
230226

231227
```javascript
232228
/**
233229
* Same thing as "fetch_promise.js", but with "async/await" syntax sugar.
234230
*/
235-
231+
236232
const fetch = require('node-fetch');
237-
233+
238234
// fetch()-API returns a Promise
239-
235+
240236
// GET request
241237
async function fetchUsers() {
242238
// This "async" function returns a Promise
243-
244239
try {
245240
const response = await fetch('https://jsonplaceholder.typicode.com/users');
246241
const users = await response.json(); // response.json() also returns a Promise
@@ -257,19 +252,18 @@
257252
}
258253
console.log();
259254
}
260-
255+
261256
fetchUsers();
262257
```
263258

264-
* For usage of Promises with `async/await` syntax sugar, check out `async/3-async-await/promise_async_await.js` as follows:
259+
* For usage of Promises with `async/await` syntax sugar, check out `src/async/3-promise-async-await/promise_async_await.js` as follows:
265260

266261
```javascript
267262
const { cleanRoom, removeGarbage, winIceCream } = require('../common');
268-
269-
// Sequential execution (chaining)
263+
264+
// ===== Sequential execution (chaining) =====
270265
async function myRoutine() {
271266
// This "async" function returns a Promise
272-
273267
try {
274268
let msg = await cleanRoom();
275269
console.log(msg);
@@ -282,16 +276,15 @@
282276
console.error(err);
283277
}
284278
}
285-
279+
286280
myRoutine();
287-
281+
288282
// 使得程序looks and feels like synchronous, 虽然实际上只是syntax sugar, under the
289283
// hood还是asynchronous
290-
291-
// Parallel execution
284+
285+
// ===== Parallel execution =====
292286
async function myRoutineParallel() {
293287
// This "async" function returns a Promise
294-
295288
try {
296289
const messages = await Promise.all([
297290
cleanRoom(),
@@ -304,7 +297,7 @@
304297
console.error(err);
305298
}
306299
}
307-
300+
308301
myRoutineParallel();
309302
```
310303

@@ -333,7 +326,7 @@ Node installations and versions are managed by NVM (Node Version Manager).
333326

334327
```bash
335328
$ nvm install node # Install the latest version
336-
329+
337330
# Verify that Node has been installed:
338331
$ nvm which current
339332
# /Users/Ziang_Lu/.nvm/versions/node/v13.5.0/bin/node

‎node-crash-course/src/async/1-callback/mylogger.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// TODO: Read docs about "uuidv4" module
2-
31
const EventEmitter = require('events');
42
const fs = require('fs');
53
const path = require('path');
@@ -14,7 +12,7 @@ class MyLogger extends EventEmitter {
1412
* @param {string} msg message to log
1513
*/
1614
log(msg) {
17-
// Emit a "message" event (to self)
15+
// Emit a "message" event (to itself)
1816
this.emit('message', { id: uuidv4(), message: msg });
1917
// The second parameter here (the object) will be passed to the "message"
2018
// event listener.
@@ -25,7 +23,6 @@ const txtLogger = new MyLogger();
2523
// Since MyLogger extends EventEmitter, we can register a callback function
2624
// on it for "message" event, specifically on this txtLogger object.
2725
txtLogger.on('message', data => {
28-
// "message" event listener
2926
fs.appendFile(
3027
path.join(__dirname, 'logs.txt'),
3128
JSON.stringify(data, null, 2)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../references/promise_fetch.js

‎node-crash-course/src/async/2-promise/fetch_promise.js‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎node-crash-course/src/async/2-promise/promise_advanced.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { cleanRoom, removeGarbage, winIceCream } = require('../common');
44

55
console.log('With asynchronous programming, this line is printed first.');
66

7-
// Sequential execution (chaining)
7+
// ===== Sequential execution (chaining) =====
88
cleanRoom()
99
.then(msg => {
1010
console.log(msg);
@@ -20,7 +20,7 @@ cleanRoom()
2020
})
2121
.catch(err => console.log(err));
2222

23-
// Parallel execution
23+
// ===== Parallel execution =====
2424

2525
// Example 1
2626
Promise.all([cleanRoom(), removeGarbage(), winIceCream()])
@@ -33,7 +33,7 @@ Promise.all([cleanRoom(), removeGarbage(), winIceCream()])
3333
// Promise.race([cleanRoom(), removeGarbage(), winIceCream()])
3434
// .then(value => {
3535
// console.log(value);
36-
// console.log('One finished');
36+
// console.log('First one finished');
3737
// })
3838
// .catch(err => console.log(err));
3939

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /