TGViewer
Asynchronous Programming Asynchronous Programming @asyncify · 1.97K subscribers
Post #43 5.13K
🔁 Первые 3 задания из курса по асинхронному программированию

1. Task: rewrite function to return result into sync callback:
// Change signature to: (items, callback(result))
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 });
2. Task: return an error for items with negative price. Hint: use callback-last-error-first contract:
const total = (items, callback) => {
  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 });
});
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).
// 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
  • 🔥 22
  • 👍 16
  • ❤ 10
  • 🤯 2
More from @asyncify
  1. Oct 3, 2026https://youtu.be/I5ILntVSjkQ
  2. Sep 28, 2026https://youtu.be/0GZ-Nf8yHEw
  3. Sep 26, 2026https://youtu.be/hKigdmvste0
  4. Sep 25, 2026В эту субботу будет стрим по применению AI в создании архитектуры. - архитектурный дрейф и…
  5. Sep 23, 2026https://www.youtube.com/watch?v=Bca7s7Yvhzk
  6. Sep 21, 2026https://youtu.be/el7RuL98KL8
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →