等待异步函数在 reduce() 函数中完成
Await async functions to be done in reduce() function
我的功能包括以下内容:
const newThreads = newItems.reduce( (acc, item) => {
request(item.href, function(error2, response2, html2){
if(!error2) {
const thread = cheerio.load(html2)
const today = thread('div#ignmsgbttns1').parent().parent().find("b:contains('Today')")
if(today.text()) {
acc.push(item)
}
}
})
return acc
}, [])
console.log(newThreads)
当然日志 returns 一个空数组,因为异步的东西
(request
) 在 reduce
循环中执行。
所以我想做的是:
const newThreads = await newItems.reduce( etc...
并等待 reduce
循环中的 requests
完成。
但我不知道如何正确地做到这一点。
所以我知道我必须使用 async
、await
或 promises
,但不知道该怎么做。
我认为 reduce callback
也必须是 async
但在这一点上完全不确定。
request
方法来自npm request package , they also provide some packages使用promises,但说实话,我不知道如何使用reduce
。
我很确定某处已经有类似的问题,但找不到。
如有任何帮助,我们将不胜感激。
ps:对于那些想知道 cheerio 是什么的人,这里是 link.
应用答案后的最终代码
我不得不使用 async-request package
const newThreads = newItems.reduce(async (acc, item) => {
const current = await acc;
const html2 = await requestAsync(item.href);
const thread = cheerio.load(html2.body);
const today = thread('div#ignmsgbttns1')
.parent()
.parent()
.find("b:contains('Today')");
if (today.text()) current.push(item);
return current;
}, []);
newThreads.then((res) => {
//..doing stuff with res
})
为了完成这项工作,您需要 Promise returning version.
const newThreads = newItems.reduce(async (acc, item) => { // note async
const current = await acc; // unwrap the previous Promise
try {
const html2 = await request(item.href); // unwrap request Promise
const thread = cheerio.load(html2);
const today = thread('div#ignmsgbttns1')
.parent()
.parent()
.find("b:contains('Today')");
if (today.text()) current.push(item);
} catch (error2) {
// do whatever
}
return current;
}, []);
newThreads
变量将是通过条件检查的项目数组的 Promise。
我的功能包括以下内容:
const newThreads = newItems.reduce( (acc, item) => {
request(item.href, function(error2, response2, html2){
if(!error2) {
const thread = cheerio.load(html2)
const today = thread('div#ignmsgbttns1').parent().parent().find("b:contains('Today')")
if(today.text()) {
acc.push(item)
}
}
})
return acc
}, [])
console.log(newThreads)
当然日志 returns 一个空数组,因为异步的东西
(request
) 在 reduce
循环中执行。
所以我想做的是:
const newThreads = await newItems.reduce( etc...
并等待 reduce
循环中的 requests
完成。
但我不知道如何正确地做到这一点。
所以我知道我必须使用 async
、await
或 promises
,但不知道该怎么做。
我认为 reduce callback
也必须是 async
但在这一点上完全不确定。
request
方法来自npm request package , they also provide some packages使用promises,但说实话,我不知道如何使用reduce
。
我很确定某处已经有类似的问题,但找不到。
如有任何帮助,我们将不胜感激。
ps:对于那些想知道 cheerio 是什么的人,这里是 link.
应用答案后的最终代码
我不得不使用 async-request package
const newThreads = newItems.reduce(async (acc, item) => {
const current = await acc;
const html2 = await requestAsync(item.href);
const thread = cheerio.load(html2.body);
const today = thread('div#ignmsgbttns1')
.parent()
.parent()
.find("b:contains('Today')");
if (today.text()) current.push(item);
return current;
}, []);
newThreads.then((res) => {
//..doing stuff with res
})
为了完成这项工作,您需要 Promise returning version.
const newThreads = newItems.reduce(async (acc, item) => { // note async
const current = await acc; // unwrap the previous Promise
try {
const html2 = await request(item.href); // unwrap request Promise
const thread = cheerio.load(html2);
const today = thread('div#ignmsgbttns1')
.parent()
.parent()
.find("b:contains('Today')");
if (today.text()) current.push(item);
} catch (error2) {
// do whatever
}
return current;
}, []);
newThreads
变量将是通过条件检查的项目数组的 Promise。