I have an async function like that:
JavaScript
x
10
10
1
module.exports.basketPosterInterceptor = async ({ page, currentPrice }) => {
2
const postRequestPrice = currentPrice * 1000 * 1000;
3
await page.setRequestInterception(true);
4
logger.info(`Poster enable price: ${postRequestPrice}`);
5
page.on('request', (interceptedRequest) => {
6
7
interceptedRequest.continue(data);
8
});
9
};
10
When I call this method, I am calling it like that:
JavaScript
1
2
1
await puppeteerService.basketPosterInterceptor({ page: posterPage, currentPrice: 50 });
2
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
JavaScript
1
11
11
1
module.exports.basketPosterInterceptor = ({ page, currentPrice }) => {
2
const postRequestPrice = currentPrice * 1000 * 1000;
3
return page.setRequestInterception(true).then(() => {
4
logger.info(`Poster enable price: ${postRequestPrice}`);
5
page.on('request', (interceptedRequest) => {
6
7
interceptedRequest.continue(data);
8
});
9
});
10
};
11
And:
JavaScript
1
4
1
puppeteerService.basketPosterInterceptor({ page: posterPage, currentPrice: 50 }).then(() => {
2
// your code goes here
3
});
4