File tree

3 files changed

+93
-114
lines changed

3 files changed

+93
-114
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
'use strict';
22

33
// Run and see random order
4+
// Use global counter to detect finish (bad practice)
45

56
let count = 0;
67

8+
const callbackCheck = () => {
9+
if (++count === 4) console.log('All done!');
10+
};
11+
12+
// Emulate Asynchronous calls
13+
14+
const wrapAsync = (callback) => setTimeout(
15+
callback, Math.floor((Math.random() * 1000))
16+
);
17+
18+
// Asynchronous functions
19+
20+
const readConfig = (name, callback) => wrapAsync(() => {
21+
console.log('(1) config loaded');
22+
callback(null, { name });
23+
});
24+
25+
const doQuery = (statement, callback) => wrapAsync(() => {
26+
console.log('(2) SQL query executed: ' + statement);
27+
callback(null, [{ name: 'Kiev' }, { name: 'Roma' }]);
28+
});
29+
30+
const httpGet = (url, callback) => wrapAsync(() => {
31+
console.log('(3) Page retrieved: ' + url);
32+
callback(null, '<html>Some archaic web here</html>');
33+
});
34+
35+
const readFile = (path, callback) => wrapAsync(() => {
36+
console.log('(4) Readme file loaded');
37+
callback(null, 'file content');
38+
});
39+
740
console.log('start');
841

942
readConfig('myConfig', callbackCheck);
@@ -12,45 +45,3 @@ httpGet('http://kpi.ua', callbackCheck);
1245
readFile('README.md', callbackCheck);
1346

1447
console.log('end');
15-
16-
function callbackCheck() {
17-
if (++count === 4) {
18-
console.log('All done!');
19-
}
20-
}
21-
22-
// Emulate Asynchronous calls
23-
24-
function wrapAsync(callback) {
25-
setTimeout(callback, Math.floor((Math.random() * 1000)));
26-
}
27-
28-
// Asynchronous functions
29-
30-
function readConfig(name, callback) {
31-
wrapAsync(() => {
32-
console.log('(1) config loaded');
33-
callback({ name });
34-
});
35-
}
36-
37-
function doQuery(statement, callback) {
38-
wrapAsync(() => {
39-
console.log('(2) SQL query executed: ' + statement);
40-
callback([ { name: 'Kiev' }, { name: 'Roma' } ]);
41-
});
42-
}
43-
44-
function httpGet(url, callback) {
45-
wrapAsync(() => {
46-
console.log('(3) Page retrieved: ' + url);
47-
callback('<html>Some archaic web here</html>');
48-
});
49-
}
50-
51-
function readFile(path, callback) {
52-
wrapAsync(() => {
53-
console.log('(4) Readme file loaded');
54-
callback('file content');
55-
});
56-
}
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,40 @@
22

33
// Back to order, callback hierarchy
44

5-
readConfig('myConfig', () => {
6-
selectFromDb('select * from cities', () => {
7-
getHttpPage('http://kpi.ua', () => {
8-
readFile('README.md', () => {
9-
console.log('All done!');
10-
});
11-
});
12-
});
13-
});
14-
155
// Emulate Asynchronous calls
166

17-
function wrapAsync(callback) {
18-
setTimeout(callback, Math.floor((Math.random() * 1000)));
19-
}
7+
const wrapAsync = (callback) => setTimeout(
8+
callback, Math.floor((Math.random() * 1000))
9+
);
2010

2111
// Asynchronous functions
2212

23-
function readConfig(name, callback) {
24-
wrapAsync(() => {
25-
console.log('(1) config loaded');
26-
callback({ name });
27-
});
28-
}
13+
const readConfig = (name, callback) => wrapAsync(() => {
14+
console.log('(1) config loaded');
15+
callback(null, { name });
16+
});
2917

30-
function selectFromDb(query, callback) {
31-
wrapAsync(() => {
32-
console.log('(2) SQL query executed');
33-
callback([ { name: 'Kiev' }, { name: 'Roma' } ]);
34-
});
35-
}
18+
const selectFromDb = (query, callback) => wrapAsync(() => {
19+
console.log('(2) SQL query executed');
20+
callback(null, [{ name: 'Kiev' }, { name: 'Roma' }]);
21+
});
3622

37-
function getHttpPage(url, callback) {
38-
wrapAsync(() => {
39-
console.log('(3) Page retrieved');
40-
callback('<html>Some archaic web here</html>');
41-
});
42-
}
23+
const getHttpPage = (url, callback) => wrapAsync(() => {
24+
console.log('(3) Page retrieved');
25+
callback(null, '<html>Some archaic web here</html>');
26+
});
4327

44-
function readFile(path, callback) {
45-
wrapAsync(() => {
46-
console.log('(4) Readme file loaded');
47-
callback('file content');
28+
const readFile = (path, callback) => wrapAsync(() => {
29+
console.log('(4) Readme file loaded');
30+
callback(null, 'file content');
31+
});
32+
33+
readConfig('myConfig', () => {
34+
selectFromDb('select * from cities', () => {
35+
getHttpPage('http://kpi.ua', () => {
36+
readFile('README.md', () => {
37+
console.log('All done!');
38+
});
39+
});
4840
});
49-
}
41+
});
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,43 @@
11
'use strict';
22

33
// Back to order, callback hierarchy
4+
// Use global data and decentralized control flow (bad practices)
45

56
const data = {};
6-
readConfig();
77

88
// Emulate Asynchronous calls
99

10-
function wrapAsync(callback) {
11-
setTimeout(callback, Math.floor((Math.random() * 1000)));
12-
}
10+
const wrapAsync = (callback) => setTimeout(
11+
callback, Math.floor((Math.random() * 1000))
12+
);
1313

1414
// Asynchronous functions
1515

16-
function readConfig() {
17-
wrapAsync(() => {
18-
console.log('(1) config loaded');
19-
data.config = { name: 'name' };
20-
selectFromDb();
21-
});
22-
}
23-
24-
function selectFromDb() {
25-
wrapAsync(() => {
26-
console.log('(2) SQL query executed');
27-
data.cities = [ { name: 'Kiev' }, { name: 'Roma' } ];
28-
getHttpPage();
29-
});
30-
}
31-
32-
function getHttpPage() {
33-
wrapAsync(() => {
34-
console.log('(3) Page retrieved');
35-
data.html = '<html>Some archaic web here</html>';
36-
readFile();
37-
});
38-
}
39-
40-
function readFile() {
41-
wrapAsync(() => {
42-
console.log('(4) Readme file loaded');
43-
data.readme = 'file content';
44-
console.dir(data);
45-
console.log('All done!');
46-
});
47-
}
16+
const readFile = () => wrapAsync(() => {
17+
console.log('(4) Readme file loaded');
18+
data.readme = 'file content';
19+
console.dir(data);
20+
console.log('All done!');
21+
});
22+
23+
const getHttpPage = () => wrapAsync(() => {
24+
console.log('(3) Page retrieved');
25+
data.html = '<html>Some archaic web here</html>';
26+
readFile();
27+
});
28+
29+
const selectFromDb = () => wrapAsync(() => {
30+
console.log('(2) SQL query executed');
31+
data.cities = [{ name: 'Kiev' }, { name: 'Roma' }];
32+
getHttpPage();
33+
});
34+
35+
const readConfig = () => wrapAsync(() => {
36+
console.log('(1) config loaded');
37+
data.config = { name: 'name' };
38+
selectFromDb();
39+
});
40+
41+
// Start execution
42+
43+
readConfig();

0 commit comments

Comments
 (0)