Skip to content

Commit

Permalink
Improve async loop
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Nov 21, 2019
1 parent d3ea648 commit ee3cabd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions fixtures/static/index.html
@@ -1,2 +1,3 @@
<h1>HTTP/2 Fixtures</h1>
<pre><code id="result"></code></pre>
<script src="/main.js"></script>
16 changes: 8 additions & 8 deletions fixtures/static/main.js
@@ -1,6 +1,7 @@
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]) {
Expand All @@ -19,15 +20,14 @@ async function fetchRel(rel) {

(async function() {
const books = await fetchRel("/books.jsonld?preload=/hydra:member/*/author");
result.innerText = JSON.stringify(books, null, 2);

for (let i = 0; i < books["hydra:member"].length; i++) {
books["hydra:member"][i] = await fetchRel(books["hydra:member"][i]);
books["hydra:member"][i].author = await fetchRel(
books["hydra:member"][i].author
);
}

document.write(`<pre><code>${JSON.stringify(books, null, 2)}</code></pre>`);
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;
})();

0 comments on commit ee3cabd

Please sign in to comment.