forked from cmda-minor-web/web-app-from-scratch-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Stan Ooms edited this page Feb 5, 2021
·
2 revisions
- Nadat ik de
fetch()
had geschreven wilde ik al snel refactoren omdat qua scaleability (meer api calls toevoegen) niet voldeed. Ook lukte het niet om vanaf het punt waarconst x = new Request
defetch()
aanroept, de data mee te geven. Zie hieronder geillustreerd:
class Request {
constructor(endPoint, Key, query) {
this.query = query;
this.endPoint = endPoint;
this.query = query;
this.Key = Key;
console.log("request fired");
if (query) {
async function x() {
const response = await fetch(endPoint);
return response.json();
}
x()
.then((res) => {
if (this.query === "initList") {
const responseWithQuery = {
res: res,
query: query, // add query as a property of a new object
};
events.dataArrived(responseWithQuery);
}
if (this.query === "topListByMarketCapOverview") {
const responseWithQuery = {
res: res,
query: query, // add query as a property of a new object
};
events.dataArrived(responseWithQuery);
}
})
.then((res) => {
console.log;
});
}
}
}
const onfullfilled = function (err, data, query) {
if (err) {
console.error(err);
return;
}
data = dataRefine.checkQuery(data, query);
localStorage.storeInitList(data, query);
};
class Request {
constructor(endPoint, Key, query) {
this.query = query;
this.endPoint = endPoint;
this.query = query;
this.Key = Key;
this.data = {};
fetch(`${endPoint}`)
.then((res) => {
console.log(res);
return res.json();
})
.then((data) => {
this.data = data.Data;
onfullfilled(null, this.data, this.query);
})
.catch((err) => {
onfullfilled(err);
});
}
}