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

188
Views
Await not awaiting async function

im working on a webscraper for an api, and i was trying to divide it into functions, for whatever reason the await is not awaiting for the promise to be fulfilled

const PORT = 8000
const axios = require('axios')
const cheerio = require('cheerio')
const express = require('express')
const app = express()
const cors = require('cors')
app.use(cors())

const clarinURL = 'https://www.clarin.com/economia/'

app.get('/', function (req, res) {
    res.json('This is my webscraper')
})

app.get('/results', async (req, res) => {
    let results = await cheerioPick(clarinURL, '.content-nota', 'h2', 'a', 'https://www.clarin.com')
    console.log(results)
    res.send(results)
})

async function cheerioPick(url, container, titletag, urltag, domain) {
    axios(url)
    .then(response => {
        const html = response.data
        const $ = cheerio.load(html)
        const articles = []

        $(container, html).each(function () { //<-- cannot be a function expression
            const title = $(this).find(titletag).text()
            const url = domain + $(this).find(urltag).attr('href')
            articles.push({
                title,
                url
            })
        })
        console.log("articles")
        return articles
    })
    .catch(err => console.log(err))
}

app.listen(PORT, () => console.log(`server running on PORT ${PORT}`))

on the console it prints:

undefined
articles

so it's clearly not awaiting for the async function to return it's result

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The issue is that the function cheerioPick is not returning a Promise.

There are some ways to get this done. I would recomend use async/await in your axios segment so your whole code use the same approch of the asynchronous coding.

async function cheerioPick(url, container, titletag, urltag, domain) {
  try {
    const response = await axios(url);
    const html = response.data
    const $ = cheerio.load(html)
    const articles = []

    $(container, html).each(function () { //<-- cannot be a function expression
        const title = $(this).find(titletag).text()
        const url = domain + $(this).find(urltag).attr('href')
        articles.push({
            title,
            url
        })
    })
    console.log("articles")
    return articles
  } catch (err) {
    console.log(err)
  }
  
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!