我有一个类似数组的结构,它公开异步方法。异步方法调用包含 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)