mirror of
https://github.com/PeWu/topola-viewer.git
synced 2026-03-14 11:33:47 +00:00
Use async/await for async functions.
This commit is contained in:
@@ -32,37 +32,32 @@ function prepareData(
|
||||
}
|
||||
|
||||
/** Fetches data from the given URL. Uses cors-anywhere if handleCors is true. */
|
||||
export function loadFromUrl(
|
||||
export async function loadFromUrl(
|
||||
url: string,
|
||||
handleCors: boolean,
|
||||
): Promise<TopolaData> {
|
||||
const cachedData = sessionStorage.getItem(url);
|
||||
if (cachedData) {
|
||||
return Promise.resolve(JSON.parse(cachedData));
|
||||
return JSON.parse(cachedData);
|
||||
}
|
||||
const urlToFetch = handleCors
|
||||
? 'https://cors-anywhere.herokuapp.com/' + url
|
||||
: url;
|
||||
|
||||
return window
|
||||
.fetch(urlToFetch)
|
||||
.then((response) => {
|
||||
if (response.status !== 200) {
|
||||
return Promise.reject(new Error(response.statusText));
|
||||
}
|
||||
return response.text();
|
||||
})
|
||||
.then((gedcom) => {
|
||||
return prepareData(gedcom, url);
|
||||
});
|
||||
const response = await window.fetch(urlToFetch);
|
||||
if (response.status !== 200) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
const gedcom = await response.text();
|
||||
return prepareData(gedcom, url);
|
||||
}
|
||||
|
||||
/** Loads data from the given GEDCOM file contents. */
|
||||
function loadGedcomSync(
|
||||
export async function loadGedcom(
|
||||
hash: string,
|
||||
gedcom?: string,
|
||||
images?: Map<string, string>,
|
||||
) {
|
||||
): Promise<TopolaData> {
|
||||
const cachedData = sessionStorage.getItem(hash);
|
||||
if (cachedData) {
|
||||
return JSON.parse(cachedData);
|
||||
@@ -72,16 +67,3 @@ function loadGedcomSync(
|
||||
}
|
||||
return prepareData(gedcom, hash, images);
|
||||
}
|
||||
|
||||
/** Loads data from the given GEDCOM file contents. */
|
||||
export function loadGedcom(
|
||||
hash: string,
|
||||
gedcom?: string,
|
||||
images?: Map<string, string>,
|
||||
): Promise<TopolaData> {
|
||||
try {
|
||||
return Promise.resolve(loadGedcomSync(hash, gedcom, images));
|
||||
} catch (e) {
|
||||
return Promise.reject(new Error('Failed to read GEDCOM file'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user