Skip to content

Commit 84cd2e7

Browse files
committed
Improve examples
1 parent 76e28ee commit 84cd2e7

10 files changed

Lines changed: 27 additions & 24 deletions

JavaScript/1-callback.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
const add = (a, b) => a + b;
44
const sum = (a, b, callback) => callback(a + b);
55

6-
console.log('Use add: ' + add(5, 2));
7-
sum(5, 2, console.log.bind(null, 'Use sum:'));
6+
console.log('add(5, 2) =', add(5, 2));
7+
sum(5, 2, console.log.bind(null, 'sum(5, 2) ='));

JavaScript/2-callback-use.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fs = require('fs');
44

55
fs.readFile('./1-callback.js', 'utf8', (err, data) => {
6-
console.log({ lines: data.toString().split('\n').length });
6+
console.log({ lines: data.split('\n').length });
77
});
88

99
console.log('end');

JavaScript/3-callback-named.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
const fs = require('fs');
44

5-
const print = (err, data) => {
6-
console.log({ lines: data.toString().split('\n').length });
5+
const print = (fileName, err, data) => {
6+
console.log({ lines: data.split('\n').length });
77
};
88

99
const fileName = './1-callback.js';
1010

11-
fs.readFile(fileName, 'utf8', print);
11+
const callback = print.bind(null, fileName);
12+
fs.readFile(fileName, 'utf8', callback);

JavaScript/4-callback-timer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ const fn = () => {
44
console.log('Callback from from timer');
55
};
66

7-
setTimeout(fn, 5000);
7+
const timeout = (interval, fn) => setTimeout(fn, interval);
8+
9+
timeout(5000, fn);

JavaScript/5-timer-curry.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ const fn = () => {
1111
console.log('Callback from from timer');
1212
};
1313

14-
const setTimeoutCallbackLast = (timeout, fn) => setTimeout(fn, timeout);
14+
const timeout = (interval, fn) => setTimeout(fn, interval);
1515

16-
const timer = curry(setTimeoutCallbackLast);
16+
const timer = curry(timeout);
1717
timer(2000)(fn);
1818

19-
const timer5s = timer(2000);
20-
timer5s(fn);
19+
const timer2s = timer(2000);
20+
timer2s(fn);

JavaScript/6-listener.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const iterate = (array, listener) => {
88

99
const cities = ['Kiev', 'London', 'Beijing'];
1010

11-
const each = city => {
12-
console.log('City: ' + city);
11+
const print = city => {
12+
console.log('City:', city);
1313
};
1414

15-
iterate(cities, each);
15+
iterate(cities, print);

JavaScript/7-listener-timer.js

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

33
const iterate = (array, listener) => {
44
let counter = 0;
5+
// setImmediate or setTimeout(0) or process.nextTick
56
setInterval(() => {
67
listener(array[counter++]);
78
if (counter >= array.length) counter = 0;
@@ -10,6 +11,6 @@ const iterate = (array, listener) => {
1011

1112
const cities = ['Kiev', 'London', 'Beijing'];
1213

13-
const fn = city => console.log('Next city: ' + city);
14+
const print = city => console.log('Next city:', city);
1415

15-
iterate(cities, fn);
16+
iterate(cities, print);

JavaScript/9-event-emitter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const events = require('events');
55
const emitter = new events.EventEmitter();
66

77
emitter.on('new city', city => {
8-
console.log('Emitted city: ' + city);
8+
console.log('Emitted city:', city);
99
});
1010

1111
emitter.on('data', array => {

JavaScript/a-deferred.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const getConferences = () => {
44
let onDone = null;
55
const deferred = {
6-
data: callback => onDone = callback
6+
onData: callback => onDone = callback
77
};
88
setTimeout(() => {
99
if (onDone) onDone(['Tehran', 'Yalta', 'Potsdam']);
@@ -17,7 +17,7 @@ const conferences = getConferences();
1717

1818
console.log(conferences);
1919

20-
conferences.data(list => {
20+
conferences.onData(list => {
2121
console.log(list);
2222
});
2323

JavaScript/b-errors.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const adder = value => {
77
value += a;
88
if (value >= add.maxValue) {
99
setImmediate(() => {
10-
add.maxEvent(new Error('max value reached'), value);
10+
const err = new Error('max value reached');
11+
add.maxEvent(err, value);
1112
});
1213
}
1314
return add;
@@ -25,10 +26,8 @@ const adder = value => {
2526

2627
// error-first
2728
const maxReached = (err, value) => {
28-
if (err) {
29-
console.log('value: ' + value);
30-
//throw err;
31-
}
29+
if (err) throw err;
30+
console.log('value:', value);
3231
};
3332

3433
try {

0 commit comments

Comments
 (0)