Changeset View
Changeset View
Standalone View
Standalone View
assets/src/utils/functions.js
Show First 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | if (closable) { | ||||
closeButton = | closeButton = | ||||
`<button type="button" class="close" data-dismiss="alert" aria-label="Close"> | `<button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||||
<span aria-hidden="true">×</span> | <span aria-hidden="true">×</span> | ||||
</button>`; | </button>`; | ||||
extraClasses = 'alert-dismissible'; | extraClasses = 'alert-dismissible'; | ||||
} | } | ||||
return `<div class="alert alert-${type} ${extraClasses}" role="alert">${message}${closeButton}</div>`; | return `<div class="alert alert-${type} ${extraClasses}" role="alert">${message}${closeButton}</div>`; | ||||
} | } | ||||
export function isValidURL(string) { | |||||
try { | |||||
new URL(string); | |||||
} catch (_) { | |||||
return false; | |||||
} | |||||
return true; | |||||
} | |||||
export async function isArchivedOrigin(originPath) { | |||||
if (!isValidURL(originPath)) { | |||||
// Not a valid URL, return immediately | |||||
return false; | |||||
} else { | |||||
const response = await fetch(Urls.api_1_origin(originPath)); | |||||
return response.ok && response.status === 200; // Success response represents an archived origin | |||||
} | |||||
} | |||||
anlambert: Could you use the [async/await](https://developer.mozilla.org/en… | |||||
Done Inline Actions
anlambert: > For the record, I am currently in the process of migrating all code using promises to… | |||||
Done Inline ActionsThanks @anlambert . I will be changing this. It is much more readable with await jayeshv: Thanks @anlambert . I will be changing this. It is much more readable with await | |||||
Done Inline ActionsYou should also simplify the code of that function with async/await, something like: export async function isArchivedOrigin(originPath) { if (!isValidURL(originPath)) { // Not a valid URL, return immediately return false; } else { const response = await fetch(Urls.api_1_origin(originPath)); return response.ok && response.status === 200; } } anlambert: You should also simplify the code of that function with async/await, something like… |
Could you use the async/await syntax of modern javascript in that function instead ?
This way you could return a boolean instead to simplify newly added code in doSearch function.
For the record, I am currently in the process of migrating all code using promises to async/await as it improves readability.