Skip to content

num42/swift-AsyncValueWithError

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

AsyncValueWithError

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.

Requirements

  • iOS 15+ / macOS 12+
  • Swift 5.10+

Installation

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.

Usage

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>

License

MIT

About

A small generic state enum for async values with errors (idle/loading/success/error).

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages