|
2 | 2 |
|
3 | 3 | // Emulate Asynchronous calls |
4 | 4 |
|
5 | | -function wrapAsync(callback) { |
6 | | - setTimeout(callback, Math.floor((Math.random() * 1000))); |
7 | | -} |
| 5 | +const wrapAsync = (callback) => setTimeout( |
| 6 | + callback, Math.floor((Math.random() * 1000)) |
| 7 | +); |
| 8 | + |
| 9 | +const isWeekend = () => !(new Date().getDay() % 6); |
8 | 10 |
|
9 | 11 | // Asynchronous functions |
10 | 12 |
|
11 | | -function readConfig(name) { |
12 | | - return new Promise((resolve, reject) => { |
13 | | - wrapAsync(() => { |
14 | | - console.log('(1) config loaded'); |
15 | | - resolve({ name }); |
16 | | - // reject(new Error('Promise fails')); |
17 | | - }); |
| 13 | +const readConfig = (name) => new Promise((resolve, reject) => { |
| 14 | + wrapAsync(() => { |
| 15 | + console.log('(1) config loaded'); |
| 16 | + if (!isWeekend()) resolve({ name }); |
| 17 | + else reject(new Error('Promises will resolve next working day')); |
18 | 18 | }); |
19 | | -} |
20 | | - |
21 | | -function doQuery(statement) { |
22 | | - return new Promise((resolve, reject) => { |
23 | | - wrapAsync(() => { |
24 | | - console.log('(2) SQL query executed: ' + statement); |
25 | | - resolve([ { name: 'Kiev' }, { name: 'Roma' } ]); |
26 | | - }); |
| 19 | +}); |
| 20 | + |
| 21 | +const doQuery = (statement) => new Promise((resolve, reject) => { |
| 22 | + wrapAsync(() => { |
| 23 | + console.log('(2) SQL query executed: ' + statement); |
| 24 | + if (!isWeekend()) resolve([{ name: 'Kiev' }, { name: 'Roma' }]); |
| 25 | + else reject(new Error('Promises will resolve next working day')); |
27 | 26 | }); |
28 | | -} |
29 | | - |
30 | | -function httpGet(url) { |
31 | | - return new Promise((resolve, reject) => { |
32 | | - wrapAsync(() => { |
33 | | - console.log('(3) Page retrieved: ' + url); |
34 | | - resolve('<html>Some archaic web here</html>'); |
35 | | - }); |
| 27 | +}); |
| 28 | + |
| 29 | +const httpGet = (url) => new Promise((resolve, reject) => { |
| 30 | + wrapAsync(() => { |
| 31 | + console.log('(3) Page retrieved: ' + url); |
| 32 | + if (!isWeekend()) resolve('<html>Some archaic web here</html>'); |
| 33 | + else reject(new Error('Promises will resolve next working day')); |
36 | 34 | }); |
37 | | -} |
38 | | - |
39 | | -function readFile(path) { |
40 | | - return new Promise((resolve, reject) => { |
41 | | - wrapAsync(() => { |
42 | | - console.log('(4) Readme file loaded: ' + path); |
43 | | - resolve('file content'); |
44 | | - }); |
| 35 | +}); |
| 36 | + |
| 37 | +const readFile = (path) => new Promise((resolve, reject) => { |
| 38 | + wrapAsync(() => { |
| 39 | + console.log('(4) Readme file loaded: ' + path); |
| 40 | + if (!isWeekend()) resolve('file content'); |
| 41 | + else reject(new Error('Promises will resolve next working day')); |
45 | 42 | }); |
46 | | -} |
| 43 | +}); |
47 | 44 |
|
48 | 45 | // Usage |
49 | 46 |
|
|
0 commit comments