简体   繁体   中英

How can i check a function result after multi seconds in node.js?

i am intercepting page request i want to wait for special request for 20 second and after that chek response? if i use setTimeout 0r set interval, request send after 20 second but i want on page load request send and code after that wait for the response for 20 second this is robot code and speed isnot matter for it

   page.on('response',async (response)=>{
     if(response.url().endWith('whatever'){
           x=response.status===200
   }
page.on('response',async (response)=>{

     await sleep(20000);

     if(response.url().endWith('whatever')){
           x=response.status===200
     }
})

i use promises in my case i wait a promise that inside it intercept request and anyway resolve after 20 see my code in the following

 let response_received=await new Promise((resolve,reject)=>{
                   page.on('response',async (response)=>{
                       if (response.url().endsWith('quickDailySalesReport')){
                           console.log(response);
                          if ( response.status()===200){
                                await sleep(2000);
                                resolve(true)
                          }
                       }
                     });
                    setTimeout(()=>resolve(false),20000)
               });
                console.log(response_received);
               if (response_received){//do something}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM