-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.js
More file actions
46 lines (39 loc) · 1.06 KB
/
page.js
File metadata and controls
46 lines (39 loc) · 1.06 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
import { get } from "./mod.ts";
export async function page(props) {
if (!props.table) {
throw ReferenceError("Missing params.table");
}
let cursor = false;
let finished = false;
function next() {
// signal completion
if (finished) {
return {
done: true,
};
}
// copy in props each invocation (limit and table)
let params = { ...props };
// if the cursor is truthy add that value to params
if (cursor) {
params.cursor = cursor;
}
return new Promise(function sigh(resolve, reject) {
get(params).then(function got(result) {
if (result && result.cursor) {
cursor = result.cursor;
resolve({ value: result, done: false });
} else {
finished = true; // important! and weird yes. we'll miss the last page otherwise
resolve({ value: result, done: false });
}
}).catch(reject);
});
}
// yay
let asyncIterator = { next };
let asyncIterable = {
[Symbol.asyncIterator]: () => asyncIterator,
};
return asyncIterable;
}