You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-30
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ SwiftTask
10
10
-`task.progress()`'s `progressClosure` type changed from `Progress -> Void` to `(oldProgress: Progress?, newProgress: Progress) -> Void`
11
11
-`task.then(fulfilledClosure)` is renamed to `task.success()`
12
12
-`task.catch(catchClosure)` is renamed to `task.failure()`
13
-
-`task.then()` is no longer used for fulfilled-only handling (this will improve Swift type-inference)
13
+
-`task.then()` is now used for completed (either fulfilled or rejected) case only, and **no longer used for fulfilled-only handling** (this will improve Swift type-inference)
14
14
15
15
16
16
## Example
@@ -45,10 +45,10 @@ let task = Task<Float, String, NSError> { progress, fulfill, reject, configure i
In order to pipeline future `task.value` or `task.errorInfo` (tuple of `(error: Error?, isCancelled: Bool)`) via `onComplete()`/`onSuccess()`/`onFailure()`, you have to call `fulfill(value)` and/or `reject(error)` inside `initClosure`.
134
+
In order to pipeline future `task.value` or `task.errorInfo` (tuple of `(error: Error?, isCancelled: Bool)`) via `then()`/`success()`/`failure()`, you have to call `fulfill(value)` and/or `reject(error)` inside `initClosure`.
135
135
136
136
Optionally, you can call `progress(progressValue)` multiple times before calling `fulfill`/`reject` to transfer `progressValue` outside of the `initClosure`, notifying it to `task` itself.
137
137
@@ -150,50 +150,50 @@ configure.cancel = { [weak player] in
150
150
}
151
151
```
152
152
153
-
### task.onProgress(_ progressClosure:) -> task
153
+
### task.progress(_ progressClosure:) -> task
154
154
155
155
```swift
156
-
task.onProgress { (oldValue: Progress?, newValue: Progress) in
157
-
println(newValue)
156
+
task.progress { (oldProgress: Progress?, newProgress: Progress) in
157
+
println(newProgress)
158
158
return
159
-
}.onSuccess { ... }
159
+
}.success { ... }
160
160
```
161
161
162
-
`task.onProgress(progressClosure)` will add `progressClosure` to observe `progressValue` which is notified from inside previous `initClosure`. This method will return **same task**, so it is useful to chain with forthcoming `onComplete`/`onSuccess`/`onFailure`.
162
+
`task.progress(progressClosure)` will add `progressClosure` to observe old/new `progressValue` which is notified from inside previous `initClosure`. This method will return **same task**, so it is useful to chain with forthcoming `then`/`success`/`failure`.
163
163
164
-
### task.onComplete(_completeClosure:) -> newTask
164
+
### task.then(_thenClosure:) -> newTask
165
165
166
-
`task.onComplete(completeClosure)` will return a new task where `completeClosure` will be invoked when `task` is either **fulfilled** or **rejected**.
166
+
`task.then(thenClosure)` will return a new task where `thenClosure` will be invoked when `task` is either **fulfilled** or **rejected**.
167
167
168
168
This case is similar to JavaScript's `promise.then(onFulfilled, onRejected)`.
169
169
170
-
`completeClosure` can be two types of closure form:
// let task2 will be fulfilled with value "\(value!) Swift"
199
199
let task2 = ...
@@ -202,42 +202,42 @@ This case is similar to JavaScript's `promise.then(onFulfilled, onRejected)`.
202
202
else {
203
203
return someOtherTask
204
204
}
205
-
}.onSuccess { (value: String) -> Void in
205
+
}.success { (value: String) -> Void in
206
206
println("\(value)") // Hello Swift
207
207
return"
208
208
}
209
209
```
210
210
211
-
### task.onSuccess(_ successClosure:) -> newTask
211
+
### task.success(_ successClosure:) -> newTask
212
212
213
-
Similar to `onComplete()` method, `task.onSuccess(successClosure)` will return a new task, but this time, `successClosure` will be invoked when task is **only fulfilled**.
213
+
Similar to `then()` method, `task.success(successClosure)` will return a new task, but this time, `successClosure` will be invoked when task is **only fulfilled**.
214
214
215
215
This case is similar to JavaScript's `promise.then(onFulfilled)`.
216
216
217
217
```swift
218
218
// let task will be fulfilled with value "Hello"
219
219
220
-
task.onSuccess { (value: String) ->Stringin
220
+
task.success { (value: String) ->Stringin
221
221
return"\(value) World"
222
-
}.onSuccess { (value: String) ->Voidin
222
+
}.success { (value: String) ->Voidin
223
223
println("\(value)") // Hello World
224
224
return"
225
225
}
226
226
```
227
227
228
-
### task.onFailure(_ failureClosure:) -> newTask
228
+
### task.failure(_ failureClosure:) -> newTask
229
229
230
-
Just the opposite of `onSuccess()`, `task.onFailure(failureClosure)` will return a new task where `failureClosure` will be invoked when task is **only rejected/cancelled**.
230
+
Just the opposite of `success()`, `task.failure(failureClosure)` will return a new task where `failureClosure` will be invoked when task is **only rejected/cancelled**.
231
231
232
232
This case is similar to JavaScript's `promise.then(undefined, onRejected)` or `promise.catch(onRejected)`.
233
233
234
234
```swift
235
235
// let task will be rejected with error "Oh My God"
236
236
237
-
task.onSuccess { (value: String) -> Void in
237
+
task.success { (value: String) -> Void in
238
238
println("\(value)") // never reaches here
239
239
return
240
-
}.onFailure { (error: NSError?, isCancelled: Bool) -> Void in
240
+
}.failure { (error: NSError?, isCancelled: Bool) -> Void in
0 commit comments