Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not possible to use foreach in async function?

Why can't I use foreach when using async functions? I always thought they just do the same thing.

 let query = try? await store.collection("feedposts").getDocuments()
 let documents = query?.documents ?? []

This works perfectly:

for document in documents {
    try? await store.collection("feedposts").document(document.documentID).collection("locked").document(uid).delete()
}

While this doesn't:

documents.forEach { document in
    try? await store.collection("feedposts").document(document.documentID).collection("locked").document(uid).delete()
}
like image 259
AdamLeet Avatar asked Dec 18 '25 06:12

AdamLeet


1 Answers

for x in y is a language feature and executes in the current scope. So the await is also executed in the current scope.

But the forEach method is, well, a method that takes a specific argument:

func forEach(_ body: (Self.Element) throws -> Void) rethrows

You pass it a block, and if you look at the signature of body you see that it's lacking the async keyword. The block is thus not (guaranteed to be) executed in an async environment. But you can only use await in an async scope, which body is not guaranteeing to the compiler and it thus doesn't allow you to use await here.

If you want a forEach method that supports async/await, you need to implement it yourself. Whether Swift is going to provide an async forEach is debated in the Swift project.

like image 102
DarkDust Avatar answered Dec 21 '25 00:12

DarkDust