I'm getting my head around the Async Await functionality and I'm stuck on a fallback scenario involving a feed. In my refactoring a viewModel attempts to get a feed from either the network or if that fails a default fallback.
The problem my head is having is that async await is supposed to remove the nested closure issues, yet I still think it's needed in a fallback use case.
func fetch() async throws -> [Feed]? {
var feed: [Feed]?
do {
feed = try await remoteLoader.load()
} catch {
// Pass error up the chain for handling
do {
feed = try await localLoader.load()
} catch {
// Pass error up the chain for handling
}
}
return feed
}
Is this the intended approach for fallbacks?
An alternative that I came up with would be to attempt to load the local first and then replace it with the Remote if that succeeds.
func fetch() async throws -> [Feed]? {
var feed: [Feed]?
do {
feed = try await localLoader.load()
feed = try await remoteLoader.load()
} catch {
throw FeedRepositoryError.FetchFeed(error)
}
return feed
}