Search code examples
javascriptnode.jssettimeout

Cannot get javascript node.js to run setTimeout every 1.5 seconds


I have a simple javascript file I'm running in node. I'm just trying to get the loop to run once every 1.5 seconds. I'm using setTimeout (thought about using sleep) but cannot get it to run.

I'm trying to set the delay for the for loop located at for (let [key, value] of Object.entries(rows)) { in the code below.

The query runs, however, it never honors the 1.5 second delay I have specified.

Many thanks in advance.

const fs = require('fs');
const papa = require('papaparse');
const axios = require('axios');
const apiKey = 'mySecret'

// console.log(papa);
let data = undefined;
const file = './FileWithColumnData.csv';
let content = fs.readFileSync(file, 'utf8');
let rows;

papa.parse(content, {
  header: true,
  delimiter: ',',
  complete: function(results) {
    rows = results.data

    usingFile = 'results.csv'
    if(fs.existsSync(usingFile)) {
      fs.unlinkSync(usingFile)    
    }
    
    const headerRow = 'Account Number,Name,Primary Street,Primary City,Primary State,Primary ZIP Code,District Number\n';
    fs.appendFileSync(usingFile, headerRow);

    for (let [key, value] of Object.entries(rows)) {
      setTimeout(function () {
        let newLine = '';
        let districtNumber = ''
        const address = value['Primary Street'] + ' ' + value['Primary City'] + ', ' + value['Primary State'] + ' ' + value['Primary ZIP Code']
        const addressEncoded = encodeURI(address)
        const axiosUrl = 'https://www.googleapis.com/civicinfo/v2/representatives?key=' + apiKey + '&address=' + addressEncoded

        axios.get(axiosUrl)
          .then((res) => {
            let _this = this
            const offices = res.data.offices;
            for (let [key2, value2] of Object.entries(offices)) {
              if (value2['name'] === 'Cook County Commissioner') {
                
                const districtVal = value2['divisionId']
                
                districtNumber =  districtVal.length == 63 ? districtVal.slice(-1) : districtVal.slice(-2)
                
                newLine = value['Name'] + ',' + value['Primary Street'] + ',' + value['Primary City'] + ',' + value['Primary State'] + ',' + value['Primary ZIP Code'] + ',' + districtNumber + '\n'
                fs.appendFileSync(usingFile, newLine);
              }
            }
          })
          .catch(function (error) {
            // handle error
            newLine = value['Name'] + ',' + value['Primary Street'] + ',' + value['Primary City'] + ',' + value['Primary State'] + ',' + value['Primary ZIP Code'] + ',Not Found\n'
            fs.appendFileSync(usingFile, newLine);
          })

      }, 1500);
    }
  }
});

Solution

  • You reaaaaaally need to learn how to use async/await and completely drop the .then() syntax, it'll make your life so much easier :)

    const papa = require('papaparse');
    const axios = require('axios');
    const apiKey = 'mySecret'
    
    import { promises } from "fs";
    
    const file = './FileWithColumnData.csv';
    let content = fs.readFileSync(file, 'utf8');
    let rows;
    
    const complete = async (results) => {
        rows = results.data;
    
        usingFile = 'results.csv'
    
        if (await promises.exists(usingFile)) {
            await promises.unlink(usingFile);
        }
    
        const headerRow = 'Account Number,Name,Primary Street,Primary City,Primary State,Primary ZIP Code,District Number\n';
        await appendFile(usingFile, headerRow);
    
        for (let [key, value] of Object.entries(rows)) {
    
            await new Promise(r => setTimeout(r, 1500));
    
            let newLine = '';
            let districtNumber = '';
            const address = value['Primary Street'] + ' ' + value['Primary City'] + ', ' + value['Primary State'] + ' ' + value['Primary ZIP Code'];
            const addressEncoded = encodeURI(address);
            const axiosUrl = 'https://www.googleapis.com/civicinfo/v2/representatives?key=' + apiKey + '&address=' + addressEncoded;
    
            try {
                const res = await axios.get(axiosUrl);
            } catch (error) {
                // handle error
                newLine = value['Name'] + ',' + value['Primary Street'] + ',' + value['Primary City'] + ',' + value['Primary State'] + ',' + value['Primary ZIP Code'] + ',Not Found\n'
                await appendFile(usingFile, newLine);
                continue;
            }
    
            const offices = res.data.offices;
    
            for (let [key2, value2] of Object.entries(offices)) {
                if (value2['name'] === 'Cook County Commissioner') {
    
                    const districtVal = value2['divisionId']
    
                    districtNumber = districtVal.length == 63 ? districtVal.slice(-1) : districtVal.slice(-2)
    
                    newLine = value['Name'] + ',' + value['Primary Street'] + ',' + value['Primary City'] + ',' + value['Primary State'] + ',' + value['Primary ZIP Code'] + ',' + districtNumber + '\n'
                    await fs.appendFile(usingFile, newLine);
                }
            }
        }
    }
    
    papa.parse(content, {
        header: true,
        delimiter: ',',
        complete
    });