Design comparison
Solution retrospective
First time using API
Community feedback
- @dwhensonPosted over 2 years ago
Hey @titocs,
Lovely job here. The app works well š
One thing I would suggest in your JS is to add a check to ensure the API has responded correctly, and render an error message if not. This isn't too tricky, and I would just change the first
then
to be something like:.then(response => response.ok ? response.json() : throw new Error("API Failed")
Your current
catch
would work in the case of some failures, but checking theok
property on the response object is more robust. I would suggest you include some fallback text inside your that would render in case of any error. Something like:adviceTag.innerHTML = `Sorry! That didn't work!`; adviceQuote.innerHTML = `Please try again later and hopefully it'll be fixed.`;
Cheers š
Dave
0@titocsPosted over 2 years agoHi @dwhenson , thanks for your feedback. but when i try to use conditional like you said (see:
.then(response => response.ok ? response.json() : throw new Error("API Failed")
), there is error that say "Unexpected token throw"0@dwhensonPosted over 2 years ago@titocs sorry, this code, might work better:
response.ok ? response.json() : Promise.reject(response);
Then in your
catch
statement, you can add:adviceTag.innerHTML = `<p>We're sorry. Something went wrong.</p>`; console.error(error);
Hopefully, that will work? Let me know how you get on.
Cheers
Dave
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord