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

161
Views
Run code after executing promise in Javascript

I am trying to save to json the values returned from indeed api. I use indeed-scraper code from github https://github.com/rynobax/indeed-scraper

My code:

... required files ...

const parsedResults = []

indeed.query(queryOptions).then(response => {

    response.forEach((res,i) => {
        setTimeout(function(){

    let url = res.url
    let resultCount = 0

    console.log(`\n  Scraping of ${url} initiated...\n`)

    const getWebsiteContent = async (url) => {
    try {
        const response = await axios.get(url)
        const $ = cheerio.load(response.data)

        ...get scraped data...
        
        parsedResults.push(metadata)
        } catch (error) {
            exportResults(parsedResults)
            console.error(error)
        }
    }
    getWebsiteContent(url)
    
}
, i*3000);
    });
    });
const outputFile = 'data.json'
const fs = require('fs');
const exportResults = (parsedResults) => {
  fs.writeFile(outputFile, JSON.stringify(parsedResults, null, 4), (err) => {
    if (err) {
      console.log(err)
    }
    console.log(`\n ${parsedResults.length} Results exported successfully to ${outputFile}\n`)
  })
}

parsedResults is not accessible in last portion of script, so to save as json file. Any help appreciated!

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!