Skip to content
Advertisement

Javascript don’t want to use await

I have an async function like that:

module.exports.basketPosterInterceptor = async ({ page, currentPrice }) => {
  const postRequestPrice = currentPrice * 1000 * 1000;
  await page.setRequestInterception(true);
  logger.info(`Poster enable price: ${postRequestPrice}`);
  page.on('request', (interceptedRequest) => {

    interceptedRequest.continue(data);
  });
};

When I call this method, I am calling it like that:

await puppeteerService.basketPosterInterceptor({ page: posterPage, currentPrice: 50 });

It works well in this way. But I don’t want to use the await keyword when calling this method. If I remove the await keyword, it waits forever in this line: await page.setRequestInterception(true); It should work on the background I don’t want to wait in that line.

How can I resolve this?

Advertisement

Answer

Use then

module.exports.basketPosterInterceptor = ({ page, currentPrice }) => {
  const postRequestPrice = currentPrice * 1000 * 1000;
  return page.setRequestInterception(true).then(() => {
    logger.info(`Poster enable price: ${postRequestPrice}`);
    page.on('request', (interceptedRequest) => {

      interceptedRequest.continue(data);
    });
  });
};

And:

puppeteerService.basketPosterInterceptor({ page: posterPage, currentPrice: 50 }).then(() => {
  // your code goes here
});
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement