Skip to content

Commit 4c28ff2

Browse files
committed
Fixed at lecture
1 parent 6eaf9ff commit 4c28ff2

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

JavaScript/1-wrap.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ const wrap = f => {
1717
// Usage
1818

1919
const func = (par1, par2) => {
20-
console.dir({ method: { par1, par2 } });
20+
console.dir({ par1, par2 });
2121
return [par1, par2];
2222
};
2323

24+
func('Uno', 'Due');
2425
const wrapped = wrap(func);
25-
wrapped('Uno', 'Due');
26+
wrapped('Tre', 'Quatro');

JavaScript/2-before-after.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const wrap = (before, after, f) => (...args) => after(f(...before(...args)));
55
// Usage
66

77
const func = (par1, par2) => {
8-
console.dir({ method: { par1, par2 } });
8+
console.dir({ par1, par2 });
99
return [par1, par2];
1010
};
1111

@@ -14,15 +14,15 @@ const before = (...args) => {
1414
return args;
1515
};
1616

17-
const after = (...args) => {
17+
const after = res => {
1818
console.log('after');
19-
return args;
19+
return res;
2020
};
2121

2222
const wrapped = wrap(before, after, func);
23-
wrapped('Uno', 'Due');
24-
23+
const res = wrapped('Uno', 'Due');
2524
console.dir({
25+
res,
2626
func: func.length,
2727
wrapped: wrapped.length,
2828
});

JavaScript/3-callback.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const wrapFunction = f => {
2222
if (typeof callback === 'function') {
2323
args[args.length - 1] = (...args) => {
2424
console.log('Callback:', f.name);
25-
return callback(...args);
25+
const cbRes = callback(...args);
26+
console.log('Callback results:', cbRes);
27+
return cbRes;
2628
};
2729
}
2830
}
@@ -37,7 +39,8 @@ const wrapFunction = f => {
3739

3840
const cloneInterface = anInterface => {
3941
const clone = {};
40-
for (const key in anInterface) {
42+
const keys = Object.keys(anInterface);
43+
for (const key of keys) {
4144
const fn = anInterface[key];
4245
clone[key] = wrapFunction(fn);
4346
}
@@ -48,12 +51,14 @@ const cloneInterface = anInterface => {
4851

4952
const interfaceName = {
5053
methodName(par1, par2, callback) {
51-
console.dir({ method: { par1, par2 } });
54+
console.dir({ par1, par2 });
5255
callback(null, { field: 'value' });
56+
return par1;
5357
}
5458
};
5559

5660
const cloned = cloneInterface(interfaceName);
57-
cloned.methodName('Uno', 'Due', () => {
58-
console.log('Fire');
61+
cloned.methodName('Uno', 'Due', (err, data) => {
62+
console.log({ err, data });
63+
return true;
5964
});

JavaScript/6-timeout-async.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ const fn200 = timeout(200, fn);
2828

2929
setTimeout(() => {
3030
fn100('first', (err, data) => {
31-
console.log('Callback first', data);
31+
console.log('Callback', data);
3232
});
3333
fn200('second', (err, data) => {
34-
console.log('Callback second', data);
34+
console.log('Callback', data);
3535
});
3636
}, 150);

0 commit comments

Comments
 (0)