Skip to content

Commit d7d7358

Browse files
committed
Improve examples
1 parent eb30a27 commit d7d7358

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

JavaScript/2-promise.js

+38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
'use strict';
22

3+
// Pending
4+
5+
const promise1 = new Promise((resolve, reject) => {
6+
setTimeout(() => {
7+
resolve('value1');
8+
}, 0);
9+
});
10+
console.dir({ promise1 }); // Promise { <pending> }
11+
promise1.then(console.log); // 'value1' (delayed)
12+
13+
// Immediate resolve
14+
15+
const promise2 = new Promise(resolve => resolve('value2'));
16+
console.dir({ promise2 }); // Promise { 'value2' }
17+
promise2.then(console.log); // 'value2'
18+
19+
// Immediate reject
20+
21+
const promise3 = new Promise((resolve, reject) => {
22+
reject(new Error('I have no value for you'));
23+
});
24+
console.dir({ promise3 }); // Promise { <rejected> Error... }
25+
promise3.then(console.log).catch(console.log); // Error...
26+
27+
// Promise.resolve
28+
29+
const promise4 = Promise.resolve('value4');
30+
console.log({ promise4 }); // Promise { 'value4' }
31+
promise4.then(console.log); // 'value4'
32+
33+
// Promise.reject
34+
35+
const promise5 = Promise.reject(new Error('I have no value for you'));
36+
console.dir({ promise5 }); // Promise { <rejected> Error... }
37+
promise5.then(console.log).catch(console.log); // Error...
38+
39+
// Example with I/O
40+
341
const fs = require('fs');
442

543
const readFile = (filename, encoding) =>

JavaScript/3-promisify.js

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
'use strict';
22

3+
const promisify = fn => (...args) => new Promise((resolve, reject) => {
4+
args.push((err, result) => {
5+
if (err) reject(err);
6+
else resolve(result);
7+
});
8+
fn(...args);
9+
});
10+
311
const fs = require('fs');
4-
const promisify = require('./promisify');
512

6-
const readFile = promisify(fs.readFile);
13+
const readFile1 = promisify(fs.readFile);
14+
15+
readFile1('file1.txt', 'utf8')
16+
.then(data => {
17+
console.log(data.toString());
18+
return readFile1('file2.txt', 'utf8');
19+
})
20+
.then(data => {
21+
console.log(data.toString());
22+
return readFile1('file3.txt', 'utf8');
23+
})
24+
.then(data => {
25+
console.log(data.toString());
26+
})
27+
.catch(err => {
28+
console.log(err);
29+
});
30+
31+
const util = require('util');
732

8-
readFile('file1.txt', 'utf8')
33+
const readFile2 = util.promisify(fs.readFile);
34+
35+
readFile2('file1.txt', 'utf8')
936
.then(data => {
1037
console.log(data.toString());
11-
return readFile('file2.txt', 'utf8');
38+
return readFile2('file2.txt', 'utf8');
1239
})
1340
.then(data => {
1441
console.log(data.toString());
15-
return readFile('file3.txt', 'utf8');
42+
return readFile2('file3.txt', 'utf8');
1643
})
1744
.then(data => {
1845
console.log(data.toString());
46+
})
47+
.catch(err => {
48+
console.log(err);
1949
});

JavaScript/7-http.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22

3-
const httpGet = require('./get-json');
3+
const fetch = require('./6-fetch.js');
44

5-
const baseUrl = 'http://localhost:3000/';
6-
7-
httpGet(baseUrl + 'person')
8-
.then((data) => {
5+
fetch('http://localhost:3000/person')
6+
.then(data => {
97
console.log(data);
10-
return httpGet(baseUrl + 'city');
118
})
12-
.then(console.log);
9+
.catch(err => {
10+
console.error(err);
11+
});

0 commit comments

Comments
 (0)