0
let data;
await page.on('response', async (response) => {

    if (response.url().includes("https://www.ubereats.com/api/getFeedV1")) {
        const j = await response.json();
        console.log(j);
    }
});

console.log(data);

This is a code segment of a web scraper with a puppeteer library. Inside this await, I can log the j variable. But I need to assign it to the data variable. How can I do that?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 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) – nbokmans Aug 17 '21 at 17:41
  • Can you just say `data = await response.json()` ? – Ludal Aug 17 '21 at 17:46
  • `page.on` is synchronous so there's nothing to `await`. It registers a callback. If you want to wait until the callback fires, you have to promisify the callback or use `waitForResponse`. The other option is putting your `data`-dependent code inside the callback chain directly. – ggorlen Aug 17 '21 at 18:04
  • thank you all. @ggorlen I have followed your way and got success. thank you. – Chamikara Nayanajith Aug 18 '21 at 10:43
  • Awesome--feel free to share a [self answer](https://stackoverflow.com/help/self-answer) to help others with the same problem. – ggorlen Aug 18 '21 at 13:54

1 Answers1

2

as @ggorlen said I have used the following method.

const responseData = (page, url) => {
               return new Promise(resolve => {
                   page.on("response", async (response) => {
                       if (response.url().includes(url)) {
                           const json = await response.json();
                           // console.log(json);
                           resolve(json);
                       }
                   });
               });
           }
   
let data = await responseData(page, "https://www.example.com/api/getFeedV1");