Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!