Skip to content

Commit

Permalink
Improve examples code style
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Oct 31, 2024
1 parent b3e7d15 commit 2d5db1d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
5 changes: 4 additions & 1 deletion JavaScript/2-before-after.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

const wrap = (f, before, after) => (...args) => after(f(...before(...args)));
const wrap =
(f, before, after) =>
(...args) =>
after(f(...before(...args)));

// Usage

Expand Down
2 changes: 1 addition & 1 deletion JavaScript/3-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const interfaceName = {
console.dir({ par1, par2 });
callback(null, { field: 'value' });
return par1;
}
},
};

const cloned = cloneInterface(interfaceName);
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/4-wrap-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const interfaceName = {
methodAsync(par1, par2, callback) {
console.dir({ method: { par1, par2 } });
callback(null, { field: 'value' });
}
},
};

const cloned = cloneInterface(interfaceName);
Expand Down
8 changes: 4 additions & 4 deletions JavaScript/6-timeout-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const fn100 = timeout(fn, 100);
const fn200 = timeout(fn, 200);

setTimeout(() => {
fn100('first', (err, data) => {
console.log('Callback', data);
fn100('first', (error, data) => {
console.log({ callback: { error, data } });
});
fn200('second', (err, data) => {
console.log('Callback', data);
fn200('second', (error, data) => {
console.log({ callback: { error, data } });
});
}, 150);
14 changes: 10 additions & 4 deletions JavaScript/9-cancelable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

const cancelable1 = (fn) => {
const wrapper = (...args) => (fn ? fn(...args) : null);
const cancel = () => fn = null;
const cancel = () => {
fn = null;
};
return { call: wrapper, cancel };
};

// Mixin

const cancelable2 = (fn) => {
const wrapper = (...args) => (fn ? fn(...args) : null);
wrapper.cancel = () => fn = null;
wrapper.cancel = () => {
fn = null;
};
return wrapper;
};

Expand All @@ -22,14 +26,16 @@ const fn = (par) => {
console.log('Function called, par:', par);
};

{ // Return struct
{
// Return struct
const f2 = cancelable1(fn);
f2.call('first');
f2.cancel();
f2.call('second');
}

{ // Mixin
{
// Mixin
const f2 = cancelable2(fn);
f2('first');
f2.cancel();
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/b-optimzed.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const wrap = (f) => {
limit = count || 0;
counter = 0;
return this;
}
},
};

return Object.assign(wrapper, methods);
Expand Down

0 comments on commit 2d5db1d

Please sign in to comment.