Skip to content

Instantly share code, notes, and snippets.

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