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
In order to pipeline future `task.value` or `task.errorInfo` (tuple of `(error: Error?, isCancelled: Bool)`) via `then` and `catch` methods, you have to call `fulfill(value)` and `reject(error)` inside closure.
135
+
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`.
127
136
128
-
Optionally, you can call `progress(progressValue)` multiple times before calling `fulfill`/`reject` to transfer `progressValue` outside of the closure, notifying it to `task` itself.
137
+
Optionally, you can call `progress(progressValue)` multiple times before calling `fulfill`/`reject` to transfer `progressValue` outside of the `initClosure`, notifying it to `task` itself.
129
138
130
139
To add `pause`/`resume`/`cancel` functionality to your `task`, use `configure` to wrap up the original one.
131
140
@@ -142,117 +151,94 @@ configure.cancel = { [weak player] in
142
151
}
143
152
```
144
153
145
-
### task.progress(_ progressClosure:) -> task
154
+
### task.onProgress(_ progressClosure:) -> task
146
155
147
156
```swift
148
-
task.progress { (progressValue: Progress) in
149
-
println(progressValue)
157
+
task.onProgress { (oldValue: Progress?, newValue: Progress) in
158
+
println(newValue)
150
159
return
151
-
}.then { ... }
160
+
}.onSuccess { ... }
152
161
```
153
162
154
-
`task.progress(progressClosure)` will add `progressClosure` to observe `progressValue` which is notified from inside previous init-closure. This method will return same task, so it is useful to chain with forthcoming `then` and `catch`.
155
-
156
-
157
-
### task.then(_ closure:) -> newTask
163
+
`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`.
158
164
159
-
`task.then(closure)` will return a new task which behaves differently depending on what kind of `closure` is passed in.
160
-
161
-
1.`closure` used for **fulfilled only**
162
-
2.`closure` used for both **fulfilled & rejected**
163
-
164
-
#### 1. closure used for fulfilled only = `fulfilledClosure`
165
-
166
-
`fulfilledClosure` will be invoked only when `task` is only *fulfilled*.
167
-
168
-
This case is similar to JavaScript's `promise.then(onFulfilled)`.
// task2 will be fulfilled with value "\(value) Swift"
188
-
189
-
task.then { (value: String) -> Task<Float, String, NSError> in
190
-
let task2 = ... // fulfilling "\(value) Swift"
191
-
return task2
192
-
}.then { (value: String) -> Void in
193
-
println("\(value)") // Hello Swift
194
-
return"
195
-
}
196
-
```
197
-
198
-
#### 2. closure for both fulfilled & rejected = `thenClosure`
199
-
200
-
In this case, `thenClosure` will be invoked when `task` is either *fulfilled* or *rejected*. This means, `thenClosure` is mostly called in future compared to `fulfilledClosure`, which is invoked only when *fulfilled*.
167
+
`task.onComplete(completeClosure)` will return a new task where `completeClosure` will be invoked when `task` is either **fulfilled** or **rejected**.
201
168
202
169
This case is similar to JavaScript's `promise.then(onFulfilled, onRejected)`.
// let task2 will be fulfilled with value "\(value!) Swift"
200
+
let task2 = ...
232
201
return task2
233
202
}
234
203
else {
235
204
return someOtherTask
236
205
}
237
-
}.then { (value: String) -> Void in
206
+
}.onSuccess { (value: String) -> Void in
238
207
println("\(value)") // Hello Swift
239
208
return"
240
209
}
241
210
```
242
211
243
-
### task.catch(_ catchClosure:) -> newTask
212
+
### task.onSuccess(_ successClosure:) -> newTask
213
+
214
+
Similar to `onComplete()` method, `task.onSuccess(successClosure)` will return a new task, but this time, `successClosure` will be invoked when task is **only fulfilled**.
215
+
216
+
This case is similar to JavaScript's `promise.then(onFulfilled)`.
217
+
218
+
```swift
219
+
// let task will be fulfilled with value "Hello"
220
+
221
+
task.onSuccess { (value: String) ->Stringin
222
+
return"\(value) World"
223
+
}.onSuccess { (value: String) ->Voidin
224
+
println("\(value)") // Hello World
225
+
return"
226
+
}
227
+
```
228
+
229
+
### task.onFailure(_ failureClosure:) -> newTask
244
230
245
-
Similar to `task.then(fulfilledClosure)` for fulfilled only, `task.catch(catchClosure)` will invoke `catchClosure` only when `task` is either *rejected* or *cancelled*.
231
+
Just the opposite of `onSuccess()`, `task.onFailure(failureClosure)` will return a new task where `failureClosure` will be invoked when task is **only rejected/cancelled**.
246
232
247
233
This case is similar to JavaScript's `promise.then(undefined, onRejected)` or `promise.catch(onRejected)`.
248
234
249
235
```swift
250
-
// task will be rejected with error "Oh My God"
236
+
// let task will be rejected with error "Oh My God"
`Task.all(tasks)` is a new task that performs all `tasks` simultaneously and will be:
264
250
265
-
- fulfilled when **all tasks will be fulfilled**
266
-
- rejected when **any of the task will be rejected**
251
+
- fulfilled when **all tasks are fulfilled**
252
+
- rejected when **any of the task is rejected**
267
253
268
254
### Task.any(_ tasks:) -> newTask
269
255
270
256
`Task.any(tasks)` is an opposite of `Task.all(tasks)` which will be:
271
257
272
-
- fulfilled when **any of the task will be fulfilled**
273
-
- rejected when **all tasks will be rejected**
258
+
- fulfilled when **any of the task is fulfilled**
259
+
- rejected when **all tasks are rejected**
274
260
275
261
### Task.some(_ tasks:) -> newTask
276
262
277
-
`Task.some(tasks)` is a new task that performs all `tasks` without internal rejection, and is fulfilled with given `tasks`'s fulfilled values. Note that this new task will also become *fulfilled* with empty value-array, even though all `tasks` are rejected.
263
+
`Task.some(tasks)` is a new task that performs all `tasks` without internal rejection, and is fulfilled with given `tasks`'s fulfilled values. Note that this new task **will be fulfilled with empty value-array, even though all `tasks` are rejected.**
0 commit comments