forked from HowProgrammingWorks/AsynchronousProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-step-by-step.js
More file actions
43 lines (32 loc) · 912 Bytes
/
4-step-by-step.js
File metadata and controls
43 lines (32 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
// Back to order
// Use global data and decentralized control flow (bad practices)
const data = {};
// Emulate asynchronous calls
const wrapAsync = (fn) => (...args) => setTimeout(
() => fn(...args), Math.floor(Math.random() * 1000)
);
// Asynchronous functions
const readFile = wrapAsync(() => {
console.log('(4) Readme file loaded');
data.readme = 'file content';
console.dir(data);
console.log('All done!');
});
const getHttpPage = wrapAsync(() => {
console.log('(3) Page retrieved');
data.html = '<html>Some archaic web here</html>';
readFile();
});
const selectFromDb = wrapAsync(() => {
console.log('(2) SQL query executed');
data.cities = [{ name: 'Kiev' }, { name: 'Roma' }];
getHttpPage();
});
const readConfig = wrapAsync(() => {
console.log('(1) config loaded');
data.config = { name: 'name' };
selectFromDb();
});
// Start execution
readConfig();