Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion JavaScript/2-emulate-async-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const callbackCounter = (count, callback) => {
// Emulate asynchronous calls

const wrapAsync = (fn) => (...args) => setTimeout(
() => fn(...args), Math.floor(Math.random() * 1000)
fn, Math.floor(Math.random() * 1000), ...args
);

// Asynchronous functions
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/3-back-to-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Emulate asynchronous calls

const wrapAsync = (fn) => (...args) => setTimeout(
() => fn(...args), Math.floor(Math.random() * 1000)
fn, Math.floor(Math.random() * 1000), ...args
);

// Asynchronous functions
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/4-step-by-step.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const data = {};
// Emulate asynchronous calls

const wrapAsync = (fn) => (...args) => setTimeout(
() => fn(...args), Math.floor(Math.random() * 1000)
fn, Math.floor(Math.random() * 1000), ...args
);

// Asynchronous functions
Expand Down
38 changes: 25 additions & 13 deletions JavaScript/5-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,43 @@ const chain = (prev = null) => {

// Emulate asynchronous calls

const wrapAsync = (fn) => (...args) => setTimeout(
() => fn(...args), Math.floor(Math.random() * 1000)
);
const wrapAsync = (fn) => {
const delayedFn = (...args) => (
setTimeout(fn, Math.floor(Math.random() * 1000), ...args)
);
Object.defineProperty(delayedFn, 'name', {
value: fn.name
});
return delayedFn;
};

// Asynchronous functions

const readConfig = wrapAsync((name, callback) => {
const readConfigCb = (name, callback) => {
console.log('(1) config loaded');
callback(null, { name });
});
};

const selectFromDb = wrapAsync((query, callback) => {
const selectFromDbCb = (query, callback) => {
console.log('(2) SQL query executed');
callback(null, [{ name: 'Kiev' }, { name: 'Roma' } ]);
});
callback(null, [{ name: 'Kiev' }, { name: 'Roma' }]);
};

const getHttpPage = wrapAsync((url, callback) => {
const getHttpPageCb = (url, callback) => {
console.log('(3) Page retrieved');
callback(null, '<html>Some archaic web here</html>');
});
};

const readFile = wrapAsync((path, callback) => {
const readFileCb = (path, callback) => {
console.log('(4) Readme file loaded');
callback(null, 'file content');
});
};

const [readConfig, selectFromDb, getHttpPage, readFile] = [
readConfigCb,
selectFromDbCb,
getHttpPageCb,
readFileCb,
].map(wrapAsync);

// Usage

Expand Down
4 changes: 2 additions & 2 deletions JavaScript/9-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ee = new EventEmitter();
// Emulate asynchronous calls

const wrapAsync = (fn) => (...args) => setTimeout(
() => fn(...args), Math.floor(Math.random() * 1000)
fn, Math.floor(Math.random() * 1000), ...args
);

// Asynchronous functions
Expand All @@ -18,7 +18,7 @@ const readConfig = wrapAsync((name) => {

const doQuery = wrapAsync((statement) => {
console.log('(2) SQL query executed: ' + statement);
ee.emit('query', [ { name: 'Kiev' }, { name: 'Roma' } ]);
ee.emit('query', [{ name: 'Kiev' }, { name: 'Roma' }]);
});

const httpGet = wrapAsync((url) => {
Expand Down