Skip to content
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

Merged
merged 4 commits into from
Jan 15, 2025
Merged

Conversation

70ray
Copy link
Collaborator

@70ray 70ray commented Jan 8, 2025

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.

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();
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

scripts/api.js Outdated Show resolved Hide resolved
scripts/api.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@jchaffraix jchaffraix left a 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.

scripts/page_browse.js Outdated Show resolved Hide resolved
@70ray
Copy link
Collaborator Author

70ray commented Jan 9, 2025

Sandbox updated.

@70ray 70ray marked this pull request as draft January 9, 2025 20:18
@70ray
Copy link
Collaborator Author

70ray commented Jan 9, 2025

It's not doing quite what it should in the real application.

@70ray 70ray marked this pull request as ready for review January 9, 2025 20:40
@70ray
Copy link
Collaborator Author

70ray commented Jan 9, 2025

Sandbox updated again.

Copy link
Member

@cpeel cpeel left a 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.

@cpeel cpeel merged commit 18dce22 into DistributedProofreaders:master Jan 15, 2025
12 checks passed
@70ray 70ray deleted the async_await branch January 17, 2025 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants