ASYNCIFY Telegram 43
🔁 Первые 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



tgoop.com/asyncify/43
Create:
Last Update:

🔁 Первые 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

BY Asynchronous Programming


Share with your friend now:
tgoop.com/asyncify/43

View MORE
Open in Telegram


Telegram News

Date: |

There have been several contributions to the group with members posting voice notes of screaming, yelling, groaning, and wailing in different rhythms and pitches. Calling out the “degenerate” community or the crypto obsessives that engage in high-risk trading, Co-founder of NFT renting protocol Rentable World emiliano.eth shared this group on his Twitter. He wrote: “hey degen, are you stressed? Just let it out all out. Voice only tg channel for screaming”. In 2018, Telegram’s audience reached 200 million people, with 500,000 new users joining the messenger every day. It was launched for iOS on 14 August 2013 and Android on 20 October 2013. The optimal dimension of the avatar on Telegram is 512px by 512px, and it’s recommended to use PNG format to deliver an unpixelated avatar. Those being doxxed include outgoing Chief Executive Carrie Lam Cheng Yuet-ngor, Chung and police assistant commissioner Joe Chan Tung, who heads police's cyber security and technology crime bureau. Developing social channels based on exchanging a single message isn’t exactly new, of course. Back in 2014, the “Yo” app was launched with the sole purpose of enabling users to send each other the greeting “Yo.”
from us


Telegram Asynchronous Programming
FROM American