-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
main.js
33 lines (27 loc) · 928 Bytes
/
main.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
const apiURL = "https://localhost:3000";
const cache = {};
const result = document.getElementById('result');
async function fetchRel(rel) {
// Prevent fetching twice the same relation
if (cache[rel]) {
return cache[rel];
}
// use a Promise to wait for pushed relation in the local cache
let res;
cache[rel] = new Promise((resolve) => { res = resolve });
const resp = await fetch(apiURL + rel, { credentials: "include" });
const json = await resp.json();
res(json);
return json;
}
(async function() {
const books = await fetchRel(`/books.jsonld?preload="/hydra:member/*/author"`);
result.innerText = JSON.stringify(books, null, 2);
books["hydra:member"].forEach(async (bookId, i) => {
const book = await fetchRel(bookId);
book.author = await fetchRel(book.author);
books["hydra:member"][i] = book;
result.innerText = JSON.stringify(books, null, 2);
});
return books;
})();