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

117
Views
SetTimeout as time of API call completion

I am trying to scrape prosthetists on the NPI public API. Currently, I am resolving a promise after 8 seconds before moving on to the next batch of 200 results and scraping them. I think this is a simplistic approach and I am curious if there is a way to track the time of the API call to finish, in this case where the console.log('The "data to append" was appended to file!), and then move on to the next iteration in the loop? Sometimes 8 seconds is enough time, and sometimes it is way too much time. What options do I have?

Here is my code:

const fs = require("fs");
const axios = require("axios");

const limit = 10000;

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

const getDoctors = async (website) => {
  const requestOptions = {
    method: "GET",

    redirect: "follow",
  };

  try {
    console.log(`scraping ${website}`);
    const { data } = await axios.get(website, requestOptions);

    await fs.readFile(
      "./customer.json",
      "utf8",
      async function (err, beepboop) {
        if (err) console.log("Oh no an error!");

        if (beepboop) {
          const json = JSON.parse(beepboop);
          json.push(data.results);

          await fs.writeFile(
            "./customer.json",
            JSON.stringify(json),
            function (err) {
              if (err) throw err;
              console.log('The "data to append" was appended to file!');
            }
          );
        } else {
          console.log("Creating file ===============================");
          await fs.writeFile(
            "./customer.json",
            JSON.stringify(data.results),
            function (err) {
              if (err) throw err;
              console.log('The "data to append" was appended to file!');
            }
          );
        }
      }
    );
  } catch (err) {
    console.log(err);
  }
};

const scrapeDoctors = async () => {
  for (let skipNumber = 0; skipNumber < limit; skipNumber += 200) {
    let url = `https://npiregistry.cms.hhs.gov/api/?version=2.1&taxonomy_description=prosthetist&limit=200&skip=${skipNumber}`;
    getDoctors(url);
    await sleep(8000);
  }
};
scrapeDoctors();
about 4 years ago · Juan Pablo Isaza
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!