|
2 | 2 |
|
3 | 3 | // Emulate asynchronous calls
|
4 | 4 |
|
5 |
| -const wrapAsync = (callback) => setTimeout( |
| 5 | +const wrapAsync = callback => setTimeout( |
6 | 6 | callback, Math.floor((Math.random() * 1000))
|
7 | 7 | );
|
8 | 8 |
|
9 | 9 | const isWeekend = () => !(new Date().getDay() % 6);
|
10 | 10 |
|
11 | 11 | // Asynchronous functions
|
12 | 12 |
|
13 |
| -const readConfig = (name) => new Promise((resolve, reject) => { |
| 13 | +const readConfig = name => new Promise((resolve, reject) => { |
14 | 14 | wrapAsync(() => {
|
15 | 15 | console.log('(1) config loaded');
|
16 | 16 | if (!isWeekend()) resolve({ name });
|
17 | 17 | else reject(new Error('Promises will resolve next working day'));
|
18 | 18 | });
|
19 | 19 | });
|
20 | 20 |
|
21 |
| -const doQuery = (statement) => new Promise((resolve, reject) => { |
| 21 | +const doQuery = statement => new Promise((resolve, reject) => { |
22 | 22 | wrapAsync(() => {
|
23 | 23 | console.log('(2) SQL query executed: ' + statement);
|
24 | 24 | if (!isWeekend()) resolve([{ name: 'Kiev' }, { name: 'Roma' }]);
|
25 | 25 | else reject(new Error('Promises will resolve next working day'));
|
26 | 26 | });
|
27 | 27 | });
|
28 | 28 |
|
29 |
| -const httpGet = (url) => new Promise((resolve, reject) => { |
| 29 | +const httpGet = url => new Promise((resolve, reject) => { |
30 | 30 | wrapAsync(() => {
|
31 | 31 | console.log('(3) Page retrieved: ' + url);
|
32 | 32 | if (!isWeekend()) resolve('<html>Some archaic web here</html>');
|
33 | 33 | else reject(new Error('Promises will resolve next working day'));
|
34 | 34 | });
|
35 | 35 | });
|
36 | 36 |
|
37 |
| -const readFile = (path) => new Promise((resolve, reject) => { |
| 37 | +const readFile = path => new Promise((resolve, reject) => { |
38 | 38 | wrapAsync(() => {
|
39 | 39 | console.log('(4) Readme file loaded: ' + path);
|
40 | 40 | if (!isWeekend()) resolve('file content');
|
@@ -48,10 +48,10 @@ Promise.resolve()
|
48 | 48 | .then(readConfig.bind(null, 'myConfig'))
|
49 | 49 | .then(doQuery.bind(null, 'select * from cities'))
|
50 | 50 | .then(httpGet.bind(null, 'http://kpi.ua'))
|
51 |
| -.catch((err) => console.log('Reject reason (1): ' + err.message)) |
| 51 | +.catch(err => console.log('Reject reason (1): ' + err.message)) |
52 | 52 | .then(readFile.bind(null, 'README.md'))
|
53 |
| -.catch((err) => console.log('Reject reason (2): ' + err.message)) |
54 |
| -.then((data) => { |
| 53 | +.catch(err => console.log('Reject reason (2): ' + err.message)) |
| 54 | +.then(data => { |
55 | 55 | console.log('Done');
|
56 | 56 | console.dir({ data });
|
57 | 57 | });
|
0 commit comments