Skip to content

Instantly share code, notes, and snippets.

@sainecy
sainecy / RunBlocking.swift
Last active October 10, 2023 03:08
Run asynchronous blocks synchronous in swift. As Kotlin runBlocking. ONLY FOR TEST PURPOSES. Async code MUST BE RUNS ONLY ASYNCHRONOUSLY IN PRODUCTION!
import Foundation
private final class RunBlocking<T, Failure: Error> {
fileprivate var value: Result<T, Failure>? = nil
}
extension RunBlocking where Failure == Never {
func runBlocking(_ operation: @Sendable @escaping () async -> T) -> T {
Task {
let task = Task(operation: operation)