【发布时间】:2020-10-08 03:15:00
【问题描述】:
我正在尝试使用async library 处理二维数组并处理每个展平项目。像这样:
import { each } from 'async';
let results: any[] = [];
await each(chats, async (chat) =>
await each(chat.participants, async (participant) => {
console.log('Before pause');
// do something with participant
results.push(fn(participant));
await this.timeout(2500);
console.log('After pause');
})
);
console.log('Finished');
return results;
在哪里
let chats = [
{
participants: ['1', '2']
},
{
participants: ['3', '4']
}
]
timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
但是,console.log('Finished') 永远不会执行。我在这段代码中尝试了很多async 和await 的变体,但无法使其正常工作。我有一个使用纯异步/等待的工作版本,但想使用异步库。
我做错了什么?
请注意,我确实在不使用异步库的情况下使用 async/await 进行了这项工作,如下所示:
await Promise.all(chats.map(
async (chat) =>
await Promise.all(chat.participants
.map(async (participant) => {
console.log('Before pause');
// do something with participant
results.push(fn(participant));
await this.timeout(2500);
console.log('After pause');
})
)
));
return results;
但我想使用异步库,因为我希望使用该库中的其他函数。
【问题讨论】:
标签: javascript typescript asynchronous async.js