From 1676c3b73e1657b9e91f0ca8194855eee4138006 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 8 Nov 2023 12:06:58 -0800 Subject: [PATCH] Add reasync initializer to `Result` --- Sources/ConcurrencyExtras/Result.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Sources/ConcurrencyExtras/Result.swift diff --git a/Sources/ConcurrencyExtras/Result.swift b/Sources/ConcurrencyExtras/Result.swift new file mode 100644 index 0000000..046066b --- /dev/null +++ b/Sources/ConcurrencyExtras/Result.swift @@ -0,0 +1,14 @@ +extension Result where Failure == Swift.Error { + /// Creates a new result by evaluating an async throwing closure, capturing the returned value as + /// a success, or any thrown error as a failure. + /// + /// - Parameter body: A throwing closure to evaluate. + @_transparent + public init(catching body: () async throws -> Success) async { + do { + self = .success(try await body()) + } catch { + self = .failure(error) + } + } +}