A small, generic state enum for representing the lifecycle of an asynchronous value that may fail. Modeled as a discriminated union with four states:
.idle.loading(intermediate:).success(result:).error(error:)
The value type T and error type E are both generic, so the enum can be
reused across features that load different kinds of data.
- iOS 15+ / macOS 12+
- Swift 5.10+
Add the package to your SwiftPM dependencies:
// Package.swift
.package(url: "https://github.com/num42/swift-AsyncValueWithError", from: "0.1.0")Then add AsyncValueWithError to the target dependencies that need it.
import AsyncValueWithError
enum LoadError: Error, Equatable { case network }
var state: AsyncValueWithError<String, LoadError> = .idle
state = .loading(intermediate: nil)
state = .success(result: "Hello")
if state.isSuccess() {
print(state.success() ?? "")
}Domain-specific aliases compose cleanly:
typealias DownloadAsync<T: Equatable> = AsyncValueWithError<T, DownloadError>MIT