Concurrency Array: parallel async map

I want to switch from Combine to the new Concurrency feature.

In the documentation on Swift, this sample is used:

async let firstPhoto = downloadPhoto(named: photoNames[0])
async let secondPhoto = downloadPhoto(named: photoNames[1])
async let thirdPhoto = downloadPhoto(named: photoNames[2])

let photos = await [firstPhoto, secondPhoto, thirdPhoto]

How would you transform this code using an Array and map?

let names = ["foo", "bar", "bar"]
let tasks = { photoName in
    downloadPhoto(named: photoName)

let photos = await tasks

Check out this gist @Douglas_Gregor made a couple months ago where he implemented parallel map using async/await. It's not up to date, so the syntax has changed a bit, but Xcode should show you the errors anyways.


Thanks, I will try it!

1 Like
Terms of Service

Privacy Policy

Cookie Policy