A SwiftUI view that performs an async task when appearing and allows the caller show different views based on the success and failure cases.
This can be used with an async function that fetches some data and decodes it into a model. In the following example, we display a list of posts when they are received or show the error message if the call to fetchPosts fails.
structPost:Identifiable{letid:Stringlettitle:String}structPostsView:View{privatefunc fetchPosts()asyncthrows->[Post]{return[] // Actually implement network call!
}varbody:someView{AsyncView{tryawaitfetchPosts()} initial:{ProgressView()} success:{ posts inSuccess(posts: posts)} failure:{ error inFailure(error: error)}}structSuccess:View{letposts:[Post]varbody:someView{List(posts){ post inText(post.title)}}}structFailure:View{leterror:Errorvarbody:someView{Text(error.localizedDescription)}}}