【发布时间】:2021-02-05 09:26:06
【问题描述】:
我有以下代码
const getCompanies = async (searchURL) => {
const html = await rp(baseURL + searchURL);
const businessMap = cheerio('a.business-name', html).map(async (i, e) => {
const phone = cheerio('p.phone', innerHtml).text();
if (phone == 123) {
exit;
}
return {
phone,
}
}).get();
return Promise.all(businessMap);
};
如果我的条件匹配,我想退出循环。有什么办法,如果条件匹配,则立即返回数据。并停止执行循环
【问题讨论】:
-
在
.map中使用async是否有原因 -
因为我在使用await,所以会有结果
-
我没有看到你在使用 await
-
见第二行 const html = await rp(baseURL + searchURL);
-
我的问题是你为什么在
.map()中使用async而不是rp
标签: node.js express promise async-await cheerio