发送参数到异步功能

时间:2018-08-05 17:28:39

标签: javascript node.js async-await

如何将数据传递给异步函数?我正在尝试获取Google搜索结果并将其传递到puppeteer中以打开页面。

for (var i in item) {
  // if the item object does not have this property as its own 
  // (for example its an inherited property) then skip this property.
  if (!item.hasOwnProperty(i)) {
    continue;
  }
  ....
}

1 个答案:

答案 0 :(得分:1)

只需发送Google通话:

 const link = new Promise(resolve => {
  google('example.com test', (err, res) => {
    if(err) {
       console.error(err);
       return;
    }
    resolve(res.links[0].link.toString());  
  });
 });

然后在异步函数中执行

 await link

在某处使用链接。

相关问题