-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use async and await for ajax #1410
Conversation
scripts/api.js
Outdated
if (!contentType || !contentType.includes("application/json")) { | ||
throw new AjaxError("Incorrect response type", AJAX_ERROR_CODES.INCORRECT_RESPONSE_TYPE); | ||
} | ||
const responseData = await response.json(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we probably shouldn't call .json()
until after we confirm the response is ok
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to decode the result even if it is not ok so this avoids doing it twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seconding the suggestions by @chrismiceli around checking for HTTP errors and potential exceptions in .json
.
Sandbox updated. |
It's not doing quite what it should in the real application. |
Sandbox updated again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is entirely outside my wheelhouse and I am happy to defer to @chrismiceli and @jchaffraix who have approved it.
Our current supported browsers can use async and await which need FF 52, Chrome 55, Safari 10.3.
This should make the code more comprehensible.
Sandbox at: https://www.pgdp.org/~rp31/c.branch/async_await
It should only affect pages using the page browser.