-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample08.ts
More file actions
46 lines (38 loc) · 1.5 KB
/
example08.ts
File metadata and controls
46 lines (38 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
interface AsyncCommand {
execute(): Promise<void>;
undo?(): Promise<void>;
}
class FetchExchangeRateCommand implements AsyncCommand {
constructor(private date: string) { }
async execute(): Promise<void> {
try {
const url = `https://api.privatbank.ua/p24api/exchange_rates?json&date=${this.date}`;
const response = await fetch(url);
const data = await response.json();
const usd = data.exchangeRate.find((rate: any) => rate.currency === "USD");
if (usd) {
console.log(
`Дата: ${this.date} | USD/UAH: купівля — ${usd.purchaseRate.toFixed(2)}, продаж — ${usd.saleRate.toFixed(2)}`
);
} else {
console.warn(`Дата: ${this.date} | Курс USD відсутній`);
}
} catch (error) {
console.error(`Дата: ${this.date} | Помилка запиту`, error);
}
}
}
async function runParallel(commands: AsyncCommand[]): Promise<void> {
const results = await Promise.allSettled(commands.map(cmd => cmd.execute()));
results.forEach((result, index) => {
if (result.status === "fulfilled") {
console.log(`Команда ${index + 1}: виконана`);
} else {
console.warn(`Команда ${index + 1}: помилка`);
}
});
}
runParallel([
new FetchExchangeRateCommand("20.04.2024"),
new FetchExchangeRateCommand("20.04.2025")
]);