Node.js 从侦听器异步获取结果

Node.js 从侦听器异步获取结果,node.js,async-await,puppeteer,eventemitter,puppeteer-cluster,Node.js,Async Await,Puppeteer,Eventemitter,Puppeteer Cluster,我使用木偶玩家集群+节点js。这方面我是新手。 我有点麻烦。 我需要从站点获取XHR响应。我正在侦听页面,但无法将结果值写入变量。我需要在代码的另一部分中使用该值。 如何等待函数在侦听器中执行并将结果写入变量 dataCreation = []; function get_data (data){ dataCreation [id] = data;} await page.on('response', async (response) =>{ if (response.url ===

我使用木偶玩家集群+节点js。这方面我是新手。 我有点麻烦。 我需要从站点获取XHR响应。我正在侦听页面,但无法将结果值写入变量。我需要在代码的另一部分中使用该值。 如何等待函数在侦听器中执行并将结果写入变量

dataCreation = [];
function get_data (data){
 dataCreation [id] = data;}
await page.on('response', async (response) =>{
  if (response.url === 'your_url'){ 
    let XHR_response = await response.json();
    let array_lenght = (XHR_response).length;
    let data2 = XHR_response.objects[array_lenght-1].creationDate;
    get_data(data2);}});

await console.log(dataCreation [id];
但是dataCreation[id]是未被击败的。因为我需要等待听众并从中得到结果。我怎么做?谢谢。

类似这样的内容:

const XHR\u response=等待新承诺((解析)=>{
第页('response',checkResponse);
功能检查响应(响应){
如果(response.url()===“您的url”){
第页关闭('response',checkResponse);
解析(response.json());
}
}
});
让数组长度=XHR\u response.objects.length;
让data2=XHR\u response.objects[array\u lenght-1].creationDate;
获取_数据(数据2);
log(数据创建[id]);

我的cod中的
wait console.log(数据创建[id];
?抱歉,有)的无效语法有什么问题。我只在这里出错,然后在stackoverflow中发布。谢谢你的提问。你的问题应该有一个字母(是的,对不起,是打字错误)