Skip to content

Commit d2ea72a

Browse files
Merge pull request #19 from lightpanda-io/fetch
use fetch to get reviews
2 parents eb3c7f9 + 3450115 commit d2ea72a

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

public/campfire-commerce/script.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Fetch product data from JSON source
22
// blocked by https://github.com/lightpanda-io/browsercore/issues/187
33
// window.addEventListener("load", () => {
4+
5+
// use XHR to retrieve the product's infos.
46
const detailsXHR = new XMLHttpRequest();
57
// blocked by https://github.com/lightpanda-io/browsercore/issues/186
68
// detailsXHR.open('GET', 'json/product.json');
@@ -13,17 +15,19 @@ detailsXHR.open('GET', document.URL + 'json/product.json');
1315
};
1416
detailsXHR.send();
1517

16-
const reviewsXHR = new XMLHttpRequest();
17-
// blocked by https://github.com/lightpanda-io/browsercore/issues/186
18-
// reviewsXHR.open('GET', 'json/reviews.json');
19-
reviewsXHR.open('GET', document.URL + 'json/reviews.json');
20-
reviewsXHR.responseType = 'json';
21-
reviewsXHR.onload = function() {
22-
if (this.status === 200) {
23-
updateReviews(this.response);
18+
// use fetch to retrieve reviews.
19+
(async function () {
20+
try {
21+
const url = document.URL + 'json/reviews.json';
22+
const response = await fetch(url);
23+
if (!response.ok) {
24+
throw new Error(`Response status: ${response.status}`);
25+
}
26+
updateReviews(await response.json());
27+
} catch (error) {
28+
console.error(error.message);
2429
}
25-
};
26-
reviewsXHR.send();
30+
}());
2731

2832
// blocked by https://github.com/lightpanda-io/browsercore/issues/185
2933
//

0 commit comments

Comments
 (0)