application-amicale/utils/WebDataManager.js

34 lines
735 B
JavaScript
Raw Normal View History

/**
* Class used to get json data from the web
*/
export default class WebDataManager {
FETCH_URL: string;
lastDataFetched: Object = {};
constructor(url) {
this.FETCH_URL = url;
}
/**
* Read data from FETCH_URL and return it.
* If no data was found, returns an empty object
*
* @return {Promise<Object>}
*/
async readData() {
let fetchedData: Object = {};
try {
let response = await fetch(this.FETCH_URL);
fetchedData = await response.json();
} catch (error) {
2019-08-08 18:40:42 +02:00
throw new Error('Could not read FetchedData from server');
}
this.lastDataFetched = fetchedData;
return fetchedData;
}
}