Skip to content
Stan Ooms edited this page Feb 5, 2021 · 2 revisions

Procesverslag

experimenten

  1. 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 waar const x = new Request de fetch() 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;
        });
    }
  }
}

Is geworden:

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);
      });
  }
}
Clone this wiki locally