Skip to content

Commit 9c8a996

Browse files
committed
Improve examples
1 parent a4ecdd9 commit 9c8a996

File tree

7 files changed

+36
-18
lines changed

7 files changed

+36
-18
lines changed

JavaScript/1-wrap.js

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

3-
// const wrap = f => (...args) => f(...args);
3+
// const wrap = (f) => (...args) => f(...args);
44

55
const wrap = (f) => {
66
console.log('Wrap function:', f.name);

JavaScript/2-before-after.js

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

3-
const wrap = (before, after, f) => (...args) => after(f(...before(...args)));
3+
const wrap = (f, before, after) => (...args) => after(f(...before(...args)));
44

55
// Usage
66

@@ -19,7 +19,7 @@ const after = (res) => {
1919
return res;
2020
};
2121

22-
const wrapped = wrap(before, after, func);
22+
const wrapped = wrap(func, before, after);
2323
const res = wrapped('Uno', 'Due');
2424
console.dir({
2525
res,

JavaScript/3-callback.js

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

3-
// const wrap = (before, after, f) => (...args) => after(f(...before(...args)));
3+
// const wrap = (f, before, after) => (...args) => after(f(...before(...args)));
44

5-
// const wrapAsync = (before, after, beforeCb, afterCb, f) =>
5+
// const wrapAsync = (f, before, after, beforeCb, afterCb) =>
66
// (...args) => {
77
// const callback = args[args.length - 1];
88
// if (typeof callback === 'function') {

JavaScript/5-timeout.js

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

33
// Wrapper will prevent call after timeout
44

5-
const timeout = (msec, f) => {
5+
const timeout = (f, msec) => {
66
let timer = setTimeout(() => {
77
if (timer) console.log('Function timed out');
88
timer = null;
@@ -21,8 +21,8 @@ const fn = (par) => {
2121
console.log('Function called, par:', par);
2222
};
2323

24-
const fn100 = timeout(100, fn);
25-
const fn200 = timeout(200, fn);
24+
const fn100 = timeout(fn, 100);
25+
const fn200 = timeout(fn, 200);
2626

2727
setTimeout(() => {
2828
fn100('first');

JavaScript/6-timeout-async.js

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

33
// Wrapper will prevent call after timeout
44

5-
const timeout = (msec, f) => {
5+
const timeout = (f, msec) => {
66
let timer = setTimeout(() => {
77
if (timer) console.log('Function timedout');
88
timer = null;
@@ -22,8 +22,8 @@ const fn = (par, callback) => {
2222
callback(null, par);
2323
};
2424

25-
const fn100 = timeout(100, fn);
26-
const fn200 = timeout(200, fn);
25+
const fn100 = timeout(fn, 100);
26+
const fn200 = timeout(fn, 200);
2727

2828
setTimeout(() => {
2929
fn100('first', (err, data) => {

JavaScript/8-limit.js

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

55
const emptiness = () => {};
66

7-
const limit = (count, fn) => {
7+
const limit = (fn, count) => {
88
let counter = 0;
99
if (!fn) return emptiness;
1010
const wrapped = (...args) => {
@@ -21,7 +21,7 @@ const fn = (par) => {
2121
console.log('Function called, par:', par);
2222
};
2323

24-
const fn2 = limit(2, fn);
24+
const fn2 = limit(fn, 2);
2525

2626
fn2('first');
2727
fn2('second');

JavaScript/9-cancelable.js

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

3-
const cancelable = (fn) => {
3+
// Return struct
4+
5+
const cancelable1 = (fn) => {
6+
const wrapper = (...args) => (fn ? fn(...args) : null);
7+
const cancel = () => fn = null;
8+
return { call: wrapper, cancel };
9+
};
10+
11+
// Mixin
12+
13+
const cancelable2 = (fn) => {
414
const wrapper = (...args) => (fn ? fn(...args) : null);
515
wrapper.cancel = () => fn = null;
616
return wrapper;
@@ -12,8 +22,16 @@ const fn = (par) => {
1222
console.log('Function called, par:', par);
1323
};
1424

15-
const f2 = cancelable(fn);
25+
{ // Return struct
26+
const f2 = cancelable1(fn);
27+
f2.call('first');
28+
f2.cancel();
29+
f2.call('second');
30+
}
1631

17-
f2('first');
18-
f2.cancel();
19-
f2('second');
32+
{ // Mixin
33+
const f2 = cancelable2(fn);
34+
f2('first');
35+
f2.cancel();
36+
f2('second');
37+
}

0 commit comments

Comments
 (0)