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

179
Visualizações
Cannot save loop data from api to json file

i have problem with my code, all i need is just to save results from looping api into json file using writeFileSync. Here's my code:

function processRarity(limit) {
    var resultJson = [];
    for (var i = limit; i < limit + 100; i++) {
      const everything = async () => {
        const response = await fetch(
          "https://api-v2-mainnet.paras.id/token/asac.near::" + i
        );
        var json = await response.json();
        return json;
      };
      everything().then((res) => {
        console.log(res);
        resultJson = res;
      });
    }
    fs.writeFileSync(`${basePath}/results.json`, JSON.stringify(resultJson, null, 2));
}

when i check my json file, all i get is just:

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

0

You need to await the calls to everything().

Also, you should be pushing onto resultJson, not reassigning it each time through the loop.

const everything = async (i) => {
    const response = await fetch(
        "https://api-v2-mainnet.paras.id/token/asac.near::" + i
    );
    var json = await response.json();
    return json;
};


async function processRarity(limit) {
    var resultJson = [];
    for (var i = limit; i < limit + 100; i++) {
        let res = await everything(i);
        console.log(res);
        resultJson.push(res);
    }
    fs.writeFileSync(`${basePath}/results.json`, JSON.stringify(resultJson, null, 2));
}
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