-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpagination.js
33 lines (29 loc) · 925 Bytes
/
pagination.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
async function getItems(nextToken) {
return fetch("http://localhost:3001/getItems?nextToken=" + nextToken).then(
(res) => res.json()
);
}
async function processItem(itemId) {
return fetch("http://localhost:3001/processItem?item=" + itemId);
}
async function* loopThroughItems() {
let nextToken = "";
do {
console.log("getting items with token", nextToken);
const res = await getItems(nextToken);
nextToken = res.nextToken;
yield res.items;
} while (nextToken);
}
async function processItems(itemsSource) {
for await (const items of itemsSource) {
await Promise.all(items.map((item) => processItem(item)));
}
}
console.time("pagination");
import { compose } from "node:stream";
import { finished } from "node:stream/promises";
const itemsSource = compose(loopThroughItems());
const stream = compose(itemsSource, processItems);
await finished(stream);
console.timeEnd("pagination");