Skip to content

Commit 50c2b02

Browse files
committed
Rewrite wrapAsync and sleep for await example
1 parent 032ec46 commit 50c2b02

File tree

9 files changed

+136
-167
lines changed

9 files changed

+136
-167
lines changed

JavaScript/2-emulate-async-calls.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@ const callbackCheck = () => {
99
if (++count === 4) console.log('All done!');
1010
};
1111

12-
// Emulate Asynchronous calls
12+
// Emulate asynchronous calls
1313

14-
const wrapAsync = (callback) => setTimeout(
15-
callback, Math.floor((Math.random() * 1000))
14+
const wrapAsync = fn => (...args) => setTimeout(
15+
() => fn(...args), Math.floor((Math.random() * 1000))
1616
);
1717

1818
// Asynchronous functions
1919

20-
const readConfig = (name, callback) => wrapAsync(() => {
20+
const readConfig = wrapAsync((name, callback) => {
2121
console.log('(1) config loaded');
2222
callback(null, { name });
2323
});
2424

25-
const doQuery = (statement, callback) => wrapAsync(() => {
25+
const doQuery = wrapAsync((statement, callback) => {
2626
console.log('(2) SQL query executed: ' + statement);
2727
callback(null, [{ name: 'Kiev' }, { name: 'Roma' }]);
2828
});
2929

30-
const httpGet = (url, callback) => wrapAsync(() => {
30+
const httpGet = wrapAsync((url, callback) => {
3131
console.log('(3) Page retrieved: ' + url);
3232
callback(null, '<html>Some archaic web here</html>');
3333
});
3434

35-
const readFile = (path, callback) => wrapAsync(() => {
35+
const readFile = wrapAsync((path, callback) => {
3636
console.log('(4) Readme file loaded');
3737
callback(null, 'file content');
3838
});

JavaScript/3-back-to-order.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22

33
// Back to order, callback hierarchy
44

5-
// Emulate Asynchronous calls
5+
// Emulate asynchronous calls
66

7-
const wrapAsync = (callback) => setTimeout(
8-
callback, Math.floor((Math.random() * 1000))
7+
const wrapAsync = fn => (...args) => setTimeout(
8+
() => fn(...args), Math.floor((Math.random() * 1000))
99
);
1010

1111
// Asynchronous functions
1212

13-
const readConfig = (name, callback) => wrapAsync(() => {
13+
const readConfig = wrapAsync((name, callback) => {
1414
console.log('(1) config loaded');
1515
callback(null, { name });
1616
});
1717

18-
const selectFromDb = (query, callback) => wrapAsync(() => {
18+
const selectFromDb = wrapAsync((query, callback) => {
1919
console.log('(2) SQL query executed');
2020
callback(null, [{ name: 'Kiev' }, { name: 'Roma' }]);
2121
});
2222

23-
const getHttpPage = (url, callback) => wrapAsync(() => {
23+
const getHttpPage = wrapAsync((url, callback) => {
2424
console.log('(3) Page retrieved');
2525
callback(null, '<html>Some archaic web here</html>');
2626
});
2727

28-
const readFile = (path, callback) => wrapAsync(() => {
28+
const readFile = wrapAsync((path, callback) => {
2929
console.log('(4) Readme file loaded');
3030
callback(null, 'file content');
3131
});

JavaScript/4-no-hierarchy.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@
55

66
const data = {};
77

8-
// Emulate Asynchronous calls
8+
// Emulate asynchronous calls
99

10-
const wrapAsync = (callback) => setTimeout(
11-
callback, Math.floor((Math.random() * 1000))
10+
const wrapAsync = fn => (...args) => setTimeout(
11+
() => fn(...args), Math.floor((Math.random() * 1000))
1212
);
1313

1414
// Asynchronous functions
1515

16-
const readFile = () => wrapAsync(() => {
16+
const readFile = wrapAsync(() => {
1717
console.log('(4) Readme file loaded');
1818
data.readme = 'file content';
1919
console.dir(data);
2020
console.log('All done!');
2121
});
2222

23-
const getHttpPage = () => wrapAsync(() => {
23+
const getHttpPage = wrapAsync(() => {
2424
console.log('(3) Page retrieved');
2525
data.html = '<html>Some archaic web here</html>';
2626
readFile();
2727
});
2828

29-
const selectFromDb = () => wrapAsync(() => {
29+
const selectFromDb = wrapAsync(() => {
3030
console.log('(2) SQL query executed');
3131
data.cities = [{ name: 'Kiev' }, { name: 'Roma' }];
3232
getHttpPage();
3333
});
3434

35-
const readConfig = () => wrapAsync(() => {
35+
const readConfig = wrapAsync(() => {
3636
console.log('(1) config loaded');
3737
data.config = { name: 'name' };
3838
selectFromDb();

JavaScript/5-chain.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
function chain(prev = null) {
3+
const chain = (prev = null) => {
44
console.log('Create element');
55
const cur = () => {
66
console.log('Reverse from ' + (cur.fn ? cur.fn.name : 'null'));
@@ -29,50 +29,42 @@ function chain(prev = null) {
2929
});
3030
};
3131
return cur;
32-
}
32+
};
3333

34-
// Emulate Asynchronous calls
34+
// Emulate asynchronous calls
3535

36-
function wrapAsync(callback) {
37-
setTimeout(callback, Math.floor((Math.random() * 1000)));
38-
}
36+
const wrapAsync = fn => (...args) => setTimeout(
37+
() => fn(...args), Math.floor((Math.random() * 1000))
38+
);
3939

4040
// Asynchronous functions
4141

42-
function readConfig(name, callback) {
43-
wrapAsync(() => {
44-
console.log('(1) config loaded');
45-
callback(null, { name });
46-
});
47-
}
42+
const readConfig = wrapAsync((name, callback) => {
43+
console.log('(1) config loaded');
44+
callback(null, { name });
45+
});
4846

49-
function selectFromDb(query, callback) {
50-
wrapAsync(() => {
51-
console.log('(2) SQL query executed');
52-
callback(null, [ { name: 'Kiev' }, { name: 'Roma' } ]);
53-
});
54-
}
47+
const selectFromDb = wrapAsync((query, callback) => {
48+
console.log('(2) SQL query executed');
49+
callback(null, [{ name: 'Kiev' }, { name: 'Roma' } ]);
50+
});
5551

56-
function getHttpPage(url, callback) {
57-
wrapAsync(() => {
58-
console.log('(3) Page retrieved');
59-
callback(null, '<html>Some archaic web here</html>');
60-
});
61-
}
52+
const getHttpPage = wrapAsync((url, callback) => {
53+
console.log('(3) Page retrieved');
54+
callback(null, '<html>Some archaic web here</html>');
55+
});
6256

63-
function readFile(path, callback) {
64-
wrapAsync(() => {
65-
console.log('(4) Readme file loaded');
66-
callback(null, 'file content');
67-
});
68-
}
57+
const readFile = wrapAsync((path, callback) => {
58+
console.log('(4) Readme file loaded');
59+
callback(null, 'file content');
60+
});
6961

7062
// Usage
7163

72-
const c1 = chain()
64+
const startChain = chain()
7365
.do(readConfig, 'myConfig')
7466
.do(selectFromDb, 'select * from cities')
7567
.do(getHttpPage, 'http://kpi.ua')
7668
.do(readFile, 'README.md');
7769

78-
c1();
70+
startChain();

JavaScript/6-promise-sequential.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
// Emulate Asynchronous calls
3+
// Emulate asynchronous calls
44

55
const wrapAsync = (callback) => setTimeout(
66
callback, Math.floor((Math.random() * 1000))

JavaScript/7-promise-all.js

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
'use strict';
22

3-
// Emulate Asynchronous calls
3+
// Emulate asynchronous calls
44

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+
);
88

99
// Asynchronous functions
1010

11-
function readConfig(name) {
12-
return new Promise(resolve => {
13-
wrapAsync(() => {
14-
console.log('(1) config loaded: ' + name);
15-
resolve({ name });
16-
});
11+
const readConfig = (name) => new Promise(resolve => {
12+
wrapAsync(() => {
13+
console.log('(1) config loaded: ' + name);
14+
resolve({ name });
1715
});
18-
}
19-
20-
function doQuery(statement) {
21-
return new Promise(resolve => {
22-
wrapAsync(() => {
23-
console.log('(2) SQL query executed: ' + statement);
24-
resolve([ { name: 'Kiev' }, { name: 'Roma' } ]);
25-
});
16+
});
17+
18+
const doQuery = (statement) => new Promise(resolve => {
19+
wrapAsync(() => {
20+
console.log('(2) SQL query executed: ' + statement);
21+
resolve([ { name: 'Kiev' }, { name: 'Roma' } ]);
2622
});
27-
}
28-
29-
function httpGet(url) {
30-
return new Promise(resolve => {
31-
wrapAsync(() => {
32-
console.log('(3) Page retrieved: ' + url);
33-
resolve('<html>Some archaic web here</html>');
34-
});
23+
});
24+
25+
const httpGet = (url) => new Promise(resolve => {
26+
wrapAsync(() => {
27+
console.log('(3) Page retrieved: ' + url);
28+
resolve('<html>Some archaic web here</html>');
3529
});
36-
}
37-
38-
function readFile(path) {
39-
return new Promise(resolve => {
40-
wrapAsync(() => {
41-
console.log('(4) Readme file loaded: ' + path);
42-
resolve('file content');
43-
});
30+
});
31+
32+
const readFile = (path) => new Promise(resolve => {
33+
wrapAsync(() => {
34+
console.log('(4) Readme file loaded: ' + path);
35+
resolve('file content');
4436
});
45-
}
37+
});
4638

4739
// Usage
4840

JavaScript/8-promise-mixed.js

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
'use strict';
22

3-
// Emulate Asynchronous calls
3+
// Emulate asynchronous calls
44

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+
);
88

99
// Asynchronous functions
1010

11-
function readConfig(name) {
12-
return new Promise(resolve => {
13-
wrapAsync(() => {
14-
console.log('(1) config loaded: ' + name);
15-
resolve({ name });
16-
});
11+
const readConfig = (name) => new Promise(resolve => {
12+
wrapAsync(() => {
13+
console.log('(1) config loaded: ' + name);
14+
resolve({ name });
1715
});
18-
}
19-
20-
function doQuery(statement) {
21-
return new Promise(resolve => {
22-
wrapAsync(() => {
23-
console.log('(2) SQL query executed: ' + statement);
24-
resolve([ { name: 'Kiev' }, { name: 'Roma' } ]);
25-
});
16+
});
17+
18+
const doQuery = (statement) => new Promise(resolve => {
19+
wrapAsync(() => {
20+
console.log('(2) SQL query executed: ' + statement);
21+
resolve([ { name: 'Kiev' }, { name: 'Roma' } ]);
2622
});
27-
}
28-
29-
function httpGet(url) {
30-
return new Promise(resolve => {
31-
wrapAsync(() => {
32-
console.log('(3) Page retrieved: ' + url);
33-
resolve('<html>Some archaic web here</html>');
34-
});
23+
});
24+
25+
const httpGet = (url) => new Promise(resolve => {
26+
wrapAsync(() => {
27+
console.log('(3) Page retrieved: ' + url);
28+
resolve('<html>Some archaic web here</html>');
3529
});
36-
}
37-
38-
function readFile(path) {
39-
return new Promise(resolve => {
40-
wrapAsync(() => {
41-
console.log('(4) Readme file loaded: ' + path);
42-
resolve('file content');
43-
});
30+
});
31+
32+
const readFile = (path) => new Promise(resolve => {
33+
wrapAsync(() => {
34+
console.log('(4) Readme file loaded: ' + path);
35+
resolve('file content');
4436
});
45-
}
37+
});
4638

4739
// Usage
4840

0 commit comments

Comments
 (0)