-1

I want to return the value returned by the function getData() I understand that I can't return a promise but how can I return only the data value obtained in the asynchronous function. A response from express

Help please...

const puppeteer = require('puppeteer');

async function getData() {
    req = 'hello';
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://www.abc.com.py/buscar/' + req);
    

    const data = await page.evaluate(() => {
        const $news = document.querySelectorAll('.item-article'); //Corresponde a la clase de Noticias de la pagina
        const dataNews = [];

        ......


            dataNews.push({
                fecha,
                enlace,
                enlace_foto,
                titulo,
                resumen,
            });
        });

        return dataNews;

    })

    return (data);
}

This Response

var express = require('express');
var router = express.Router();
var getData = require('../public/javascripts/script');

//import getData from '../public/javascripts/script';

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


router.get('/consulta', (req, res) => {
  data = getData.then(val => val);
  res.json(data);
  
})

module.exports = router;
  • Send your response in the `then` callback. – tkausl Jan 18 '22 at 00:08
  • 2
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – evolutionxbox Jan 18 '22 at 08:02

1 Answers1

0
router.get('/consulta', async (req, res) => {
  data = await getData();
  res.json(data);
})

OR

router.get('/consulta', (req, res) => {
  getData().then(data=>res.json(data));
})