なにこれ?
FutureってFutureパターンのFutureじゃないの?
なんで生成時既に値が確定してるの?
それってJustじゃね?
クロージャ内でDispatchQueueを使って非同期にしてみたけど、面倒くさい。
ということで作ってみました。
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))
}
}
これが
let future = Future<Int, Error>(on: DispatchQueue.global()) { promise in
sleep(1)
promise(.success(1))
}
こうなります。