小编Par*_*ord的帖子

在 catch 块中等待在 forEach 中失败

我有一个类似数组的结构,它公开异步方法。异步方法调用包含 try-catch 块,这些块反过来在捕获错误的情况下公开更多异步方法。我想了解为什么forEach不能很好地使用async/ await

let items = ['foo', 'bar', 'baz'];

// Desirable behavior
processForLoop(items);
/* Processing foo
 * Resolved foo after 3 seconds.
 * Processing bar
 * Resolved bar after 3 seconds.
 * Processing baz
 * Resolved baz after 3 seconds.
 */

// Undesirable behavior
processForEach(items);
/* Processing foo
 * Processing bar
 * Processing baz
 * Resolved foo after 3 seconds.
 * Resolved bar after 3 seconds.
 * Resolved baz after 3 seconds.
 */

async …
Run Code Online (Sandbox Code Playgroud)

javascript foreach try-catch async-await

2
推荐指数
2
解决办法
4429
查看次数

标签 统计

async-await ×1

foreach ×1

javascript ×1

try-catch ×1