I have a completion handler that I need to assign to a property, but I want it to execute asynchronously.
If I didn't have that requirement, I would write:
request.completionBlock = completionBlock
But since I have this requirement, I have to write this
request.completionBlock = { response, error in
DispatchQueue.main.async {
completionBlock(response, error)
}
}
which seems redundant and un-swifty.
Isn't there some easier syntax? I would like to write something like
request.completionBlock = completionBlock.map(DispatchQueue.main.async)
Can I express my need in such a simple way?