Last active
April 6, 2021 04:42
-
-
Save sorin-ref/17ff20c67d618197ca9c93b4eb4b0adc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Combine | |
import PlaygroundSupport | |
func await<T>(_ body: @escaping (@escaping (T) -> Void) -> AnyCancellable) -> T { | |
return { | |
var result: T! = nil | |
let semaphore = DispatchSemaphore(value: 0) | |
body { value in | |
result = value | |
semaphore.signal() | |
} | |
semaphore.wait() | |
return result! | |
}() | |
} | |
func async(_ body: @escaping () -> Void) { | |
DispatchQueue(label: "async").async(execute: body) | |
} | |
async { | |
let response1 = await(URLSession.shared.dataTaskPublisher(for: URL(string: "https://apple.com")!).assertNoFailure().sink) | |
print(response1.data) | |
let response2 = await(URLSession.shared.dataTaskPublisher(for: URL(string: "https://google.com")!).assertNoFailure().sink) | |
print(response2.data) | |
} | |
PlaygroundPage.current.needsIndefiniteExecution = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment