Files
sourcekit-lsp/Sources/SwiftExtensions/RunWithCleanup.swift
T
Alex Hoppen 448164aae4 Require Swift 6.3
Swift 6.3 has been released and we no longer need to support building or testing SourceKit-LSP with Swift 6.2.
2026-04-03 08:14:13 +02:00

31 lines
1009 B
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.5)
#warning("Remove this in favor of SE-0493 (Support async calls in defer bodies) when possible")
#endif
/// Run `body` and always ensure that `cleanup` gets run, independently of whether `body` threw an error or returned a
/// value.
package func run<T>(
_ body: () async throws -> T,
cleanup: () async -> Void
) async throws -> T {
do {
let result = try await body()
await cleanup()
return result
} catch {
await cleanup()
throw error
}
}