Concurrency Array: parallel async map

Hey,
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]
show(photos)

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

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

let photos = await tasks
show(photos)

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.

2 Likes

Thanks, I will try it!

1 Like
Terms of Service

Privacy Policy

Cookie Policy