-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path3-back-to-order.js
48 lines (39 loc) · 980 Bytes
/
3-back-to-order.js
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
44
45
46
47
48
'use strict';
// Back to order, callback hierarchy
readConfig('myConfig', () => {
selectFromDb('select * from cities', () => {
getHttpPage('http://kpi.ua', () => {
readFile('README.md', () => {
});
});
});
});
// Emulate Asynchronous calls
function wrapAsync(callback) {
setTimeout(callback, Math.floor((Math.random() * 1000)));
}
// Asynchronous functions
function readConfig(name, callback) {
wrapAsync(() => {
console.log('(1) config loaded');
callback({ name });
});
}
function selectFromDb(query, callback) {
wrapAsync(() => {
console.log('(2) SQL query executed');
callback([ { name: 'Kiev' }, { name: 'Roma' } ]);
});
}
function getHttpPage(url, callback) {
wrapAsync(() => {
console.log('(3) Page retrieved');
callback('<html>Some archaic web here</html>');
});
}
function readFile(path, callback) {
wrapAsync(() => {
console.log('(4) Readme file loaded');
callback('file content');
});
}