score:2

getData = async function() {
  console.log(this.state.active);
  this.setState({ data: [] });

  // my code starts here
  if (this.controller) { controller.abort() }
  this.controller = new AbortController();
  var signal = controller.signal;
  let resp = await fetch(`https://swapi.co/api/${this.state.active}/`, { signal });
  let data = await resp.json();
  let results = data.results;
  if(data.next !== null) {
    do {
      let nextResp = await fetch(data.next);
      data = await nextResp.json();
      let nextResults = data.results
      results.push(nextResults);
      results = results.reduce(function (a, b) { return a.concat(b) }, []);
    } while (data.next);
  }
  this.setState({ data: results});
}

Related Query

More Query from same tag