This is where the desire for "asynchronous handles" (@asyncHandler
) comes from. It still needs a proper pitch---and probably a better name---but the idea is that it captures the notion of a function that is called synchronously (say, from a UI) but its body is an async task. So in your button-click example you might do something like this:
@asyncHandler()
func onButtonClick(button: Int) {
let data = await download(url: url)
myView.image = Image(decoding: data)
}
with is mostly (more-optimizable) syntactic sugar for:
func onButtonClick(button: Int) {
Task.runDetached {
let data = await download(url: url)
myView.image = Image(decoding: data)
}
}
Doug