Skip to content

Commit 9065abe

Browse files
committed
Better naming
1 parent 49b1d5c commit 9065abe

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

JavaScript/4-wrap-timeout.js

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

33
// Wrapper will prevent call after timeout
44

5-
function wrapTimeout(timeout, fn) {
5+
function timeout(msec, fn) {
66
let timer = setTimeout(() => {
77
if (timer) console.log('Function timedout');
88
timer = null;
9-
}, timeout);
9+
}, msec);
1010
return (...args) => {
1111
if (timer) {
1212
timer = null;
@@ -19,8 +19,8 @@ const fn = (par) => {
1919
console.log('Function called, par: ' + par);
2020
};
2121

22-
const fn100 = wrapTimeout(100, fn);
23-
const fn200 = wrapTimeout(200, fn);
22+
const fn100 = timeout(100, fn);
23+
const fn200 = timeout(200, fn);
2424

2525
setTimeout(() => {
2626
fn100('first');

JavaScript/5-wrap-timeout-async.js

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

33
// Wrapper will prevent call after timeout
44

5-
function wrapTimeout(timeout, fn) {
5+
function timeout(msec, fn) {
66
let timer = setTimeout(() => {
77
if (timer) console.log('Function timedout');
88
timer = null;
9-
}, timeout);
9+
}, msec);
1010
return (...args) => {
1111
if (timer) {
1212
timer = null;
@@ -20,8 +20,8 @@ const fn = (par, callback) => {
2020
callback(null, par);
2121
};
2222

23-
const fn100 = wrapTimeout(100, fn);
24-
const fn200 = wrapTimeout(200, fn);
23+
const fn100 = timeout(100, fn);
24+
const fn200 = timeout(200, fn);
2525

2626
setTimeout(() => {
2727
fn100('first', (err, data) => {

JavaScript/6-wrap-once.js

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

33
// Wrapper will prevent calls > n
44

5-
const wrapOnce = (fn) => {
5+
const once = (fn) => {
66
let finished = false;
7-
const wrapper = (...args) => {
7+
return (...args) => {
88
if (finished) return;
99
finished = true;
1010
fn(...args);
1111
};
12-
return wrapper;
1312
};
1413

1514
const fn = (par) => {
1615
console.log('Function called, par: ' + par);
1716
};
1817

19-
const f = wrapOnce(fn);
18+
const f = once(fn);
2019

2120
f('first');
2221
f('second');

JavaScript/7-wrap-count.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
// Wrapper will prevent calls > n
44

5-
const wrapCount = (count, fn) => {
5+
const limit = (count, fn) => {
66
let counter = 0;
7-
const wrapper = (...args) => {
8-
if (counter === count) return;
9-
counter++;
7+
return (...args) => {
8+
if (counter++ === count) return;
109
fn(...args);
1110
};
12-
return wrapper;
1311
};
1412

1513
const fn = (par) => {
1614
console.log('Function called, par: ' + par);
1715
};
1816

19-
const fn2 = wrapCount(2, fn);
17+
const fn2 = limit(2, fn);
2018

2119
fn2('first');
2220
fn2('second');

0 commit comments

Comments
 (0)