Skip to content

Commit 9aadb4d

Browse files
committed
Optimize wrappers
1 parent af1fd96 commit 9aadb4d

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

JavaScript/8-cancelable.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const cancelable = (fn) => {
66
};
77
wrapper.cancel = () => {
88
fn = null;
9+
return wrapper;
910
};
1011
return wrapper;
1112
};

JavaScript/9-wrap.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ const wrap = (fn) => {
55
let counter = 0;
66

77
const wrapper = (...args) => {
8-
if (limit && counter++ === limit) wrapper.cancel();
9-
if (fn) return fn(...args);
8+
if (limit && counter === limit) wrapper.cancel();
9+
if (fn) {
10+
const res = fn(...args);
11+
counter++;
12+
return res;
13+
}
1014
};
1115

1216
wrapper.cancel = () => {

JavaScript/a-optimized.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ const wrap = (func) => {
88

99
const wrapper = (...args) => {
1010
if (!fn) return;
11-
if (limit && counter++ === limit) {
11+
if (limit && counter === limit) {
1212
limit = 0;
1313
counter = 0;
1414
this.cancel();
1515
return;
1616
}
17-
return fn(...args);
17+
const res = fn(...args);
18+
counter++
19+
return res;
1820
};
1921

2022
const methods = {

0 commit comments

Comments
 (0)