From 133c6b4e3df6e6ff9473df83b65e1204d664771b Mon Sep 17 00:00:00 2001 From: Vadym Markov Date: Sun, 28 May 2017 16:25:17 +0200 Subject: [PATCH 1/2] Expose asVoid function --- Playground.playground/Contents.swift | 33 ++++++++++++++++++++-------- Sources/When/Functions.swift | 10 ++++----- Sources/When/Promise.swift | 21 +++++++++--------- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/Playground.playground/Contents.swift b/Playground.playground/Contents.swift index a03b5a5..9339514 100644 --- a/Playground.playground/Contents.swift +++ b/Playground.playground/Contents.swift @@ -2,6 +2,7 @@ import Foundation import XCPlayground +import PlaygroundSupport import When enum Error: Swift.Error { @@ -14,7 +15,7 @@ Promise({ return "String" }).always({ result in print("Always") - print(result.value) + print(result.value ?? "") }).done({ value in print(value) }).fail({ error in @@ -91,21 +92,35 @@ promise4 promise4.resolve("String") +// MARK: - Recover + +let promise5 = Promise() +promise5 + .then({ value -> String in + throw Error.notFound + }) + .recover({ error -> String in + return "String" + }) + .done({ value in + print(value) + }) + // MARK: - When -let promise5 = Promise() -let promise6 = Promise() -let promise7 = Promise() +let promise6 = Promise() +let promise7 = Promise() +let promise8 = Promise() -when(promise5, promise6, promise7) +when(promise6, promise7, promise8) .done({ value1, value2, value3 in print(value1) print(value2) print(value3) }) -promise5.resolve(1) -promise6.resolve("String") -promise7.resolve(3) +promise6.resolve(1) +promise7.resolve("String") +promise8.resolve(3) -XCPlaygroundPage.currentPage.needsIndefiniteExecution = true +PlaygroundPage.current.needsIndefiniteExecution = true diff --git a/Sources/When/Functions.swift b/Sources/When/Functions.swift index 8335375..c7c6e6f 100644 --- a/Sources/When/Functions.swift +++ b/Sources/When/Functions.swift @@ -1,20 +1,20 @@ import Foundation -let mainQueue = DispatchQueue.main let backgroundQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.background) let instantQueue = DispatchQueue(label: "When.InstantQueue", attributes: []) let barrierQueue = DispatchQueue(label: "When.BarrierQueue", attributes: DispatchQueue.Attributes.concurrent) public func when(_ p1: Promise, _ p2: Promise) -> Promise<(T, U)> { - return when([p1.asVoid(), p2.asVoid()]).then(on: instantQueue) { _ in + return when([p1.asVoid(on: instantQueue), p2.asVoid(on: instantQueue)]).then(on: instantQueue) { _ in (p1.state.result!.value!, p2.state.result!.value!) } } public func when(_ p1: Promise, _ p2: Promise, _ p3: Promise) -> Promise<(T, U, V)> { - return when([p1.asVoid(), p2.asVoid(), p3.asVoid()]).then(on: instantQueue) { _ in - (p1.state.result!.value!, p2.state.result!.value!, p3.state.result!.value!) - } + return when([p1.asVoid(on: instantQueue), p2.asVoid(on: instantQueue), p3.asVoid(on: instantQueue)]) + .then(on: instantQueue, ({ _ in + (p1.state.result!.value!, p2.state.result!.value!, p3.state.result!.value!) + })) } public func when(_ promises: [Promise]) -> Promise<[T]> { diff --git a/Sources/When/Promise.swift b/Sources/When/Promise.swift index 8c5171c..0d5a65d 100644 --- a/Sources/When/Promise.swift +++ b/Sources/When/Promise.swift @@ -16,7 +16,7 @@ open class Promise { // MARK: - Initialization /// Create a promise that resolves using a synchronous closure. - public init(queue: DispatchQueue = mainQueue, _ body: @escaping (Void) throws -> T) { + public init(queue: DispatchQueue = .main, _ body: @escaping (Void) throws -> T) { state = .pending self.queue = queue @@ -31,7 +31,7 @@ open class Promise { } /// Create a promise that resolves using an asynchronous closure that can either resolve or reject. - public init(queue: DispatchQueue = mainQueue, + public init(queue: DispatchQueue = .main, _ body: @escaping (_ resolve: (T) -> Void, _ reject: (Error) -> Void) -> Void) { state = .pending self.queue = queue @@ -42,7 +42,7 @@ open class Promise { } /// Create a promise that resolves using an asynchronous closure that can only resolve. - public init(queue: DispatchQueue = mainQueue, _ body: @escaping (@escaping (T) -> Void) -> Void) { + public init(queue: DispatchQueue = .main, _ body: @escaping (@escaping (T) -> Void) -> Void) { state = .pending self.queue = queue @@ -52,7 +52,7 @@ open class Promise { } /// Create a promise with a given state. - public init(queue: DispatchQueue = mainQueue, state: State = .pending) { + public init(queue: DispatchQueue = .main, state: State = .pending) { self.queue = queue self.state = state } @@ -168,13 +168,13 @@ open class Promise { // MARK: - Then extension Promise { - public func then(on queue: DispatchQueue = mainQueue, _ body: @escaping (T) throws -> U) -> Promise { + public func then(on queue: DispatchQueue = .main, _ body: @escaping (T) throws -> U) -> Promise { let promise = Promise(queue: queue) addObserver(on: queue, promise: promise, body) return promise } - public func then(on queue: DispatchQueue = mainQueue, _ body: @escaping (T) throws -> Promise) -> Promise { + public func then(on queue: DispatchQueue = .main, _ body: @escaping (T) throws -> Promise) -> Promise { let promise = Promise(queue: queue) addObserver(on: queue, promise: promise) { value -> U? in let nextPromise = try body(value) @@ -214,8 +214,8 @@ extension Promise { update(state: state) } - func asVoid() -> Promise { - return then(on: instantQueue) { _ in return } + public func asVoid(on queue: DispatchQueue = .main) -> Promise { + return then(on: queue) { _ in return } } } @@ -225,8 +225,7 @@ extension Promise { /** Helps to recover from certain errors. Continues the chain if a given closure does not throw. */ - public func recover(on queue: DispatchQueue = mainQueue, - _ body: @escaping (Error) throws -> T) -> Promise { + public func recover(on queue: DispatchQueue = .main, _ body: @escaping (Error) throws -> T) -> Promise { let promise = Promise(queue: queue) addRecoverObserver(on: queue, promise: promise, body) return promise @@ -235,7 +234,7 @@ extension Promise { /** Helps to recover from certain errors. Continues the chain if a given closure does not throw. */ - public func recover(on queue: DispatchQueue = mainQueue, + public func recover(on queue: DispatchQueue = .main, _ body: @escaping (Error) throws -> Promise) -> Promise { let promise = Promise(queue: queue) addRecoverObserver(on: queue, promise: promise) { error -> T? in From c8c2622c31efe0b138aa62924e52362b55c65e77 Mon Sep 17 00:00:00 2001 From: Vadym Markov Date: Sun, 28 May 2017 16:26:26 +0200 Subject: [PATCH 2/2] Add docs --- Sources/When/Promise.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/When/Promise.swift b/Sources/When/Promise.swift index 0d5a65d..0cf4eef 100644 --- a/Sources/When/Promise.swift +++ b/Sources/When/Promise.swift @@ -214,6 +214,9 @@ extension Promise { update(state: state) } + /** + Returns a promise with Void as a result type. + */ public func asVoid(on queue: DispatchQueue = .main) -> Promise { return then(on: queue) { _ in return } }