diff --git a/JavaScript/2-emulate-async-calls.js b/JavaScript/2-emulate-async-calls.js index b55bcd8..e4aa2ad 100644 --- a/JavaScript/2-emulate-async-calls.js +++ b/JavaScript/2-emulate-async-calls.js @@ -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 diff --git a/JavaScript/3-back-to-order.js b/JavaScript/3-back-to-order.js index 39e953d..028b0ab 100644 --- a/JavaScript/3-back-to-order.js +++ b/JavaScript/3-back-to-order.js @@ -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 diff --git a/JavaScript/4-step-by-step.js b/JavaScript/4-step-by-step.js index 4aa8c90..bf2caa8 100644 --- a/JavaScript/4-step-by-step.js +++ b/JavaScript/4-step-by-step.js @@ -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 diff --git a/JavaScript/5-chain.js b/JavaScript/5-chain.js index ee145c3..fff89ad 100644 --- a/JavaScript/5-chain.js +++ b/JavaScript/5-chain.js @@ -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, 'Some archaic web here'); -}); +}; -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 diff --git a/JavaScript/9-events.js b/JavaScript/9-events.js index 89176fe..1debd93 100644 --- a/JavaScript/9-events.js +++ b/JavaScript/9-events.js @@ -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 @@ -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) => {