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

Convert promises to async-await #227

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions templates/switchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,18 @@ const _create_language_select = (languages) => {
* @param {Array<string>} urls
* @private
*/
const _navigate_to_first_existing = (urls) => {
const _navigate_to_first_existing = async (urls) => {
// Navigate to the first existing URL in urls.
for (const url of urls) {
fetch(url)
.then((response) => {
if (response.ok) {
window.location.href = url;
return url;
}
})
.catch((err) => {
console.error(`Error when fetching '${url}'!`);
console.error(err);
});
try {
const response = await fetch(url, { method: 'HEAD' });
if (response.ok) {
window.location.href = url;
return url;
}
} catch (err) {
console.error(`Error when fetching '${url}': ${err}`);
}
}

// if all else fails, redirect to the d.p.o root
Expand All @@ -111,7 +109,7 @@ const _navigate_to_first_existing = (urls) => {
* @returns {void}
* @private
*/
const _on_version_switch = (event) => {
const _on_version_switch = async (event) => {
if (_IS_LOCAL) return;

const selected_version = event.target.value;
Expand All @@ -127,7 +125,7 @@ const _on_version_switch = (event) => {
// 2. The current page in English with the new version
// 3. The documentation home in the current language with the new version
// 4. The documentation home in English with the new version
_navigate_to_first_existing([
await _navigate_to_first_existing([
window.location.href.replace(_CURRENT_PREFIX, new_prefix),
window.location.href.replace(_CURRENT_PREFIX, new_prefix_en),
new_prefix,
Expand All @@ -142,7 +140,7 @@ const _on_version_switch = (event) => {
* @returns {void}
* @private
*/
const _on_language_switch = (event) => {
const _on_language_switch = async (event) => {
if (_IS_LOCAL) return;

const selected_language = event.target.value;
Expand All @@ -155,7 +153,7 @@ const _on_language_switch = (event) => {
// Try the following pages in order:
// 1. The current page in the new language with the current version
// 2. The documentation home in the new language with the current version
_navigate_to_first_existing([
await _navigate_to_first_existing([
window.location.href.replace(_CURRENT_PREFIX, new_prefix),
new_prefix,
]);
Expand Down