This file contains hidden or 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
| DispatchQueue.global().async { | |
| let result = doSomething() | |
| let transformedResult = transform(result) | |
| completionHandler(transformedResult) | |
| } |
This file contains hidden or 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
| DispatchQueue.global().async { | |
| let result = doSomething() | |
| completionHandler(result) | |
| } |
This file contains hidden or 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
| DispatchQueue.global().async { | |
| let result = doSomethingAsynchronously() | |
| let transformedToAsyncFunction = transformToAsynchronous(result) | |
| DispatchQueue.global().async { | |
| let transformedValue = transformedToAsyncFunction() | |
| completionHandler(transformedValue) | |
| } | |
| } |
This file contains hidden or 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
| class Promise<T> { | |
| var task:(() -> T)? | |
| init(_ value: T) { | |
| task = { return value } | |
| } | |
| init(_ task:@escaping() -> T) { | |
| self.task = task |
This file contains hidden or 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
| func map<Destination>(_ transform:@escaping(T)->Destination) -> Promise<Destination> { | |
| return PromiseMap(container: self, transform: transform) | |
| } |
This file contains hidden or 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
| class PromiseMap<Source, Destination>: Promise<Destination> { | |
| let sourcePromise: Promise<Source> | |
| let transform: (Source)->(Destination) | |
| init(container:Promise<Source>, transform: @escaping(Source)->Destination) { | |
| self.sourcePromise = container | |
| self.transform = transform | |
| super.init() | |
| } |
This file contains hidden or 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
| originalPromise // that is the original Promise A | |
| .map(transformFunction) // this function returns a new Promise B | |
| .onCompletion {... // this onCompletion refers to Promise B | |
| // so the Promise A onCompletion is not "exposed" |
This file contains hidden or 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
| func flatMap<Destination>(_ transform:@escaping(T) -> Promise<Destination>) -> Promise<Destination> { | |
| let mappedTransform = self.map(transform) | |
| return NestedPromise(mappedTransform) | |
| } |
This file contains hidden or 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
| class NestedPromise<T>: Promise<T> { | |
| var outerPromise:Promise<Promise<T>> | |
| init(_ nestedPromises:Promise<Promise<T>>) { | |
| outerPromise = nestedPromises | |
| super.init() | |
| } | |
| override func onCompletion(_ completionHandler: @escaping (T) -> Void) { |
This file contains hidden or 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
| let nasaURL = Promise("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY") | |
| func imageURL(from urlString:String) -> URL { | |
| let url = URL(string:urlString)! | |
| let data = try! Data(contentsOf: url) | |
| let jsonDictionary = try! JSONDecoder().decode([String:String].self, from: data) | |
| let imageURL = jsonDictionary["hdurl"] as! String | |
| return URL(string:imageURL)! | |
| } |
OlderNewer