tgoop.com/asyncify/43
Create:
Last Update:
Last Update:
🔁 Первые 3 задания из курса по асинхронному программированию
1. Task: rewrite function to return result into sync callback:
// Change signature to: (items, callback(result))2. Task: return an error for items with negative price. Hint: use callback-last-error-first contract:
const total = (items) => {
let result = 0;
for (const item of items) {
result += item.price;
}
return result;
};
const electronics = [
{ name: 'Laptop', price: 1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable', price: 10 },
];
// Use new signature total(electronics, (money) => ...)
const money = total(electronics);
console.log({ money });
const total = (items, callback) => {3. Task: rewrite total function to be async with JavaScript timers. Use setInterval and clearInterval to check next item each 1 second. Calculations will be executed asynchronously because of timers. Run total twice (as in example below) but in parallel. Print debug output for each calculation step (each second).
let result = 0;
for (const item of items) {
result += item.price;
}
callback(result);
};
const electronics = [
{ name: 'Laptop', price: -1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable', price: 10 },
];
total(electronics, (money) => {
console.log({ money });
});
// Hint: example output:
// { item: { name: 'Laptop', price: 1500 } }
// { item: { name: 'Laptop', price: 1500 } }
// { item: { name: 'Keyboard', price: 100 } }
// { item: { name: 'Keyboard', price: 100 } }
// { item: { name: 'HDMI cable', price: 10 } }
// { item: { name: 'HDMI cable', price: 10 } }
// { money: 1610 }
// { money: 1610 }
const total = (items, callback) => {
let result = 0;
for (const item of items) {
console.log({ item });
if (item.price < 0) {
callback(new Error('Negative price is not allowed'));
return;
}
result += item.price;
}
callback(null, result);
};
const electronics = [
{ name: 'Laptop', price: 1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable', price: 10 },
];
total(electronics, (error, money) => {
if (error) console.error({ error });
else console.log({ money });
});
total(electronics, (error, money) => {
if (error) console.error({ error });
else console.log({ money });
});
👉 Программа курсу: https://github.com/HowProgrammingWorks/Index/blob/master/Courses/Async-2024.md
BY Asynchronous Programming
Share with your friend now:
tgoop.com/asyncify/43