-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsales.js
68 lines (48 loc) · 1.47 KB
/
sales.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import api from "api";
async function totalSales (readers, concurrency) {
if (!readers || !Array.isArray(readers) || readers.length === 0) {
throw new Error("Invalid readers argument");
}
if (!concurrency || typeof concurrency !== "number" || concurrency <= 0) {
throw new Error("Invalid concurrency argument");
}
let total = 0;
async function executor () {
while (readers.length > 0) {
const reader = readers.shift();
const result = await reader();
total += result;
}
}
const executors = [];
for (let i = 0; i < concurrency; i++) {
executors[i] = executor(i);
}
await Promise.all(executors);
return total;
}
async function atlantic () {
const sales = await api.atlantic.sales.find();
const total = sales.reduce((sum, sl) => sum + sl.amount, 0);
return total;
}
async function europe () {
const exchangeRate = 1.126;
const transactions = await api.europe.transactions.find();
let total = transactions.reduce((sum, tx) => sum + tx.payment.total, 0);
total *= exchangeRate;
return total;
}
async function kepler78b () {
const exchangeRate = 2.786;
const exports = await api.exoplanet("kepler78b").exports.find();
let total = exports.reduce((sum, ex) => sum + ex.trade.coins, 0);
total *= exchangeRate;
return total;
}
const readers = [atlantic, europe, kepler78b];
totalSales(readers, 3)
.then((total) => console.log(total))
.catch((error) => console.error(error));
// Async output:
// 500600150.46