[SWIFT] Extension that asynchronously executes closures passed to the initializer of Combine.Future

what's this?

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)
            }
        }
    }
}

How to use


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

Extension that asynchronously executes closures passed to the initializer of Combine.Future
[Swift] Lightly introduce the logic of the app that passed the selection
How to check the extension and size of uploaded files
This and that of the JDK
A memo that was soberly addicted to the request of multipart / form-data
To you who lament that the conversion of JODConverter + LibreOffice is slow