Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
How to use fetched data for argument in a method

I'm trying to make an application that gets all the planets from SWAPI and then displays them as boxes on the page. I have _load() method, which fetches the data, and _render() method, which is supposed to render the boxes. The _render() has name, terrain, population parameters.

Here is part of my code:

const box = document.createElement("div");
    box.classList.add("box");
    box.innerHTML = this._render({
      name: '',
      terrain: "placeholder",
      population: 0,
    });

    document.body.querySelector(".main").appendChild(box);

    this.emit(Application.events.READY);
  }

_load() where I fetch the data

async function getPlanets() {
      const urls = Array.from({ length: 7 },
        (v, i) => `https://swapi.boom.dev/api/planets?page=${i + 1}`);

      const promises = urls.map(url => fetch(url)
        .then(res => res.json())
        .then(data => data.results));

      const planetData = (await Promise.all(promises)).flat();
      console.log(`Results for ${planetData.length} planets downloaded...`);
      console.log('Results:', planetData);
    }
    getPlanets()
And here is the _render()

_render({ name, terrain, population }) {
    return `
<article class="media">
  <div class="media-left">
    <figure class="image is-64x64">
      <img src="${image}" alt="planet">
    </figure>
  </div>
  <div class="media-content">
    <div class="content">
    <h4>${name}</h4>
      <p>
        <span class="tag">${terrain}</span> <span class="tag">${population}</span>
        <br>
      </p>
    </div>
  </div>
</article>
    `;
  }

What I need to do is get the value for these parameters from the fetched data.

Pastebin - https://pastebin.com/hbxfHC1U

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on your pastebin snippet I've made some adjustments.

First modify your _load function so that it returns the result the result from the SWAPI request. I've removed the function that you define within it, because it seemed unnesseccary and you're already inside of an async context.

Use the returned value of _load in your constructor and pass it to _create. (Your comments stated that this was your intention, anyway). Within _create loop over the data, create your box and call the _render method by passing the properties that you need from each item.

class Application {
  constructor() {
    this._startLoading()
    this._load()
      .then(data => this._create(data))
      .then(() => this._stopLoading())
  }

  async _load() {
    const urls = Array.from({
        length: 7
      },
      (v, i) => `https://swapi.boom.dev/api/planets?page=${i + 1}`);

    const promises = urls.map(url => fetch(url)
      .then(res => res.json())
      .then(data => data.results));

    const planetData = await Promise.all(promises)
    return planetData.flat();
  }
  
    //here should be moved the rendering of the boxes, I suppose by establishing a connection with _render()
  _create(data) {
    data.forEach(({ name, terrain, population }) => {
      const box = document.createElement("div");
      box.classList.add("box");
      
      box.innerHTML = this._render({
        name,
        terrain,
        population,
      });
      
      document.body.querySelector(".main").appendChild(box);
    })
  }

  _render({ name, terrain, population, image = '' }) {
    return `
<article class="media">
  <div class="media-left">
    <figure class="image is-64x64">
      <img src="${image}" alt="planet">
    </figure>
  </div>
  <div class="media-content">
    <div class="content">
    <h4>${name}</h4>
      <p>
        <span class="tag">${terrain}</span> <span class="tag">${population}</span>
        <br>
      </p>
    </div>
  </div>
</article>
    `;
  }

  // we can leave these two alone for now
  _startLoading() {
    console.log('loading');
  }

  _stopLoading() {
    console.log('finished');
  }

}

new Application();
<main class="main"></main>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda