Isn't Future a Future of Future pattern? Why is the value already fixed at the time of generation? Isn't that just?
I tried to make it asynchronous using DispatchQueue in the closure, but it's troublesome. So I made it.
import Combine
extension Future {
convenience init<S: Scheduler>(on scheduler: S, _ attemptToFulfill: @escaping (@escaping Promise) -> Void) {
self.init { promise in
scheduler.schedule {
attemptToFulfill(promise)
}
}
}
}
let future = Future<Int, Error> { promise in
DispatchQueue.global().async {
sleep(1)
promise(.success(1))
}
}
This is
let future = Future<Int, Error>(on: DispatchQueue.global()) { promise in
sleep(1)
promise(.success(1))
}
It will be like this.
Recommended Posts