Skip to content

Instantly share code, notes, and snippets.

@jrsonline
Created June 15, 2019 14:33
Show Gist options
  • Save jrsonline/3a2346c423edf0649182fe6211673d08 to your computer and use it in GitHub Desktop.
Save jrsonline/3a2346c423edf0649182fe6211673d08 to your computer and use it in GitHub Desktop.
Sample using toBlockingArray ... and a way to publish debug output!
struct NSLoggerStream : TextOutputStream {
mutating func write(_ string: String) {
guard string.count > 0 && string != "\n" else { return }
NSLog(string)
}
}
// Test
var logger = NSLoggerStream()
let result = Publishers.Future<Int,TestError> { subscriber in
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
subscriber(.success(42))
// subscriber(.failure(TestError() )) // <== try swapping the success and failure
}
}
.print("[Test Publish/Subscribe]", to: logger )
.toBlockingResult(timeout: 5) <== try changing the timeout to 1 second
.map { $0.last! }
print("Result is \(result)", to: &logger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment