Use async/await for async functions.

This commit is contained in:
Przemek Wiech
2019-03-16 23:53:20 +01:00
parent 750cb394e7
commit 25c5438a04
4 changed files with 109 additions and 144 deletions

View File

@@ -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'));
}
}