Skip to content

Commit 709a21a

Browse files
committed
Add return and clearTimeout
1 parent 9065abe commit 709a21a

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

JavaScript/4-wrap-timeout.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ function timeout(msec, fn) {
99
}, msec);
1010
return (...args) => {
1111
if (timer) {
12+
clearTimeout(timer);
1213
timer = null;
13-
fn(...args);
14+
return fn(...args);
1415
}
1516
};
1617
}
1718

19+
// Usage:
20+
1821
const fn = (par) => {
1922
console.log('Function called, par: ' + par);
2023
};

JavaScript/5-wrap-timeout-async.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ function timeout(msec, fn) {
99
}, msec);
1010
return (...args) => {
1111
if (timer) {
12+
clearTimeout(timer);
1213
timer = null;
13-
fn(...args);
14+
return fn(...args);
1415
}
1516
};
1617
}
@@ -25,9 +26,9 @@ const fn200 = timeout(200, fn);
2526

2627
setTimeout(() => {
2728
fn100('first', (err, data) => {
28-
console.log('Callback, data: ' + data);
29+
console.log('Callback first', data);
2930
});
3031
fn200('second', (err, data) => {
31-
console.log('Callback, data: ' + data);
32+
console.log('Callback second', data);
3233
});
3334
}, 150);

0 commit comments

Comments
 (0)