Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
Using map function with Async/Await

Im learning how to use Puppeteer and the case is that when i try to map my data array object i can´t do it using the map function. is there a way map it with async/await or is there another way?

const puppeteer = require("puppeteer");
        module.exports.screenshot = async (req, res) => {
        
            let data = [
                { url: "someurl1", src: "someplace1" },
                { url: "someurl2", src: "someplace1" }
            ];
        
            let browser = await puppeteer.launch();
            let page = await browser.newPage();
        
            try {
                let files = data.map(info => (
                     await page.goto(info.url) //here i can´t await
                     await page.screenshot({ path: info.src }) //these two functions
                 ));
                await browser.close();
                res.send({
                    data: files
                })
            } catch (error) {
                console.error(`An error has occurred ${error}`);
            }
        
        }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have a callback function inside map() that is a separate block to the outer function.

To await inside you at minimum need to make that callback async as well

const files = data.map(async (info) => (
  await page.goto(info.url),
  await page.screenshot({ path: info.src })
));

If files is supposed to have the screenshot (the result of last await inside map()), I think you need to add a comma between awaits. I added it above.

A more explicit syntax might be

const files = data.map(async (info) => {
  await page.goto(info.url);
  return await page.screenshot({ path: info.src })
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda