Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jacobsapps/d8ea53e9511a3ba94e710ae31ae1e655 to your computer and use it in GitHub Desktop.
Save jacobsapps/d8ea53e9511a3ba94e710ae31ae1e655 to your computer and use it in GitHub Desktop.
actor TaskActor<T> {
private var cachedTask: Task<T, Error>?
func run(_ operation: @escaping @Sendable () async throws -> T) async throws -> T {
if cachedTask == nil {
cachedTask = Task {
try await operation()
}
}
defer { cachedTask = nil }
return try await cachedTask!.value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment