diff --git a/JavaScript/Tasks/1-sync.js b/JavaScript/Tasks/1-sync.js index 9939c4f..12a38e0 100644 --- a/JavaScript/Tasks/1-sync.js +++ b/JavaScript/Tasks/1-sync.js @@ -3,12 +3,13 @@ // Task: rewrite function to return result into sync callback // Change signature to: (items, callback(result)) -const total = (items) => { +const total = (items, callback) => { let result = 0; for (const item of items) { result += item.price; } - return result; + callback(result); + }; const electronics = [ @@ -18,5 +19,4 @@ const electronics = [ ]; // Use new signature total(electronics, (money) => ...) -const money = total(electronics); -console.log({ money }); +total(electronics, (money) => console.log(money)); diff --git a/JavaScript/Tasks/2-error.js b/JavaScript/Tasks/2-error.js index 5badbcf..e620a3a 100644 --- a/JavaScript/Tasks/2-error.js +++ b/JavaScript/Tasks/2-error.js @@ -8,7 +8,10 @@ const total = (items, callback) => { for (const item of items) { result += item.price; } - callback(result); + if(result < 0){ + callback(new Error("Price is negative"), result) + } + callback(null, result); }; const electronics = [ @@ -17,6 +20,10 @@ const electronics = [ { name: 'HDMI cable', price: 10 }, ]; -total(electronics, (money) => { +total(electronics, (err, money) => { + if(err){ + console.error(err.message, money) + throw err + } console.log({ money }); }); diff --git a/JavaScript/Tasks/3-async.js b/JavaScript/Tasks/3-async.js index 626fe64..1f94823 100644 --- a/JavaScript/Tasks/3-async.js +++ b/JavaScript/Tasks/3-async.js @@ -16,17 +16,30 @@ // { money: 1610 } // { money: 1610 } -const total = (items, callback) => { +const total = async (items, callback) => { let result = 0; - for (const item of items) { - console.log({ check: { item } }); - if (item.price < 0) { - callback(new Error('Negative price is not allowed')); - return; - } - result += item.price; - } - callback(null, result); + let index = 0; + await new Promise((res, rej)=> { + const timerId = setInterval(()=>{ + const item = items[index]; + if(!item){ + clearInterval(timerId) + res(); + return + } + console.log({ check: { item } }); + if (item.price < 0) { + rej(new Error('Negative price is not allowed')); + clearInterval(timerId) + return; + } + result += item.price; + index+=1; + }, 1000) + + + }).then(()=>callback(null, result)).catch(err=>callback(err)) + }; const electronics = [ diff --git a/JavaScript/Tasks/4-hell.js b/JavaScript/Tasks/4-hell.js index 0985775..a197773 100644 --- a/JavaScript/Tasks/4-hell.js +++ b/JavaScript/Tasks/4-hell.js @@ -47,16 +47,22 @@ const budget = (limit) => { const wallet = budget(1650); -getPurchase((purchase) => { - let amount = 0; - iterateGroups(purchase, (group) => { - groupTotal(group, (subtotal) => { - wallet.withdraw(subtotal, (success) => { - if (success) amount += subtotal; - wallet.rest((balance) => { - console.log({ success, amount, subtotal, balance }); - }); - }); +const processGroupTotal = (amount) => (subtotal) => { + wallet.withdraw(subtotal, (success) => { + if (success) amount += subtotal; + wallet.rest((balance) => { + console.log({ success, amount, subtotal, balance }); }); }); -}); +} + +const processIterateGroups = (amount) =>(group) => { + groupTotal(group, processGroupTotal(amount) ); +} + +const processPurchase = (purchase) => { + let amount = 0; + iterateGroups(purchase, processIterateGroups(amount)); +} + +getPurchase(processPurchase); diff --git a/JavaScript/Tasks/5-errors.js b/JavaScript/Tasks/5-errors.js index f5f9769..43549ef 100644 --- a/JavaScript/Tasks/5-errors.js +++ b/JavaScript/Tasks/5-errors.js @@ -14,34 +14,39 @@ const calculateSubtotal = (goods, callback) => { let amount = 0; for (const item of goods) { if (typeof item.name !== 'string') { - throw new Error('Noname in item in the bill'); + return callback(new Error('Noname in item in the bill')) } if (typeof item.price !== 'number') { - throw new Error(`${item.name} price expected to be number`); + return callback(new Error(`${item.name} price expected to be number`)); } if (item.price < 0) { - throw new Error(`Negative price for ${item.name}`); + return callback(new Error(`Negative price for ${item.name}`)); } amount += item.price; } - callback(amount); + callback(null, amount); }; const calculateTotal = (order, callback) => { const expenses = new Map(); let total = 0; - const calc = (groupName) => (amount) => { - total += amount; - expenses.set(groupName, amount); + const calc = (groupName) => (err, amount) => { + if(err){ + return callback(err); + } else { + total += amount; + expenses.set(groupName, amount); + } + }; for (const groupName in order) { const goods = order[groupName]; calculateSubtotal(goods, calc(groupName)); if (total > MAX_PURCHASE) { - throw new Error('Total is above the limit'); + return callback(new Error('Total is above the limit')); } } - return callback({ total, expenses }); + return callback(null, { total, expenses }); }; const purchase = { @@ -56,12 +61,16 @@ const purchase = { ], }; -try { + console.log(purchase); - calculateTotal(purchase, (bill) => { - console.log(bill); + calculateTotal(purchase, (err, bill) => { + + if(err){ + console.log('Error detected'); + console.error(err); + } else { + console.log(bill); + } + }); -} catch (error) { - console.log('Error detected'); - console.error(error); -} +