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.
134
+
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
135
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.
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.
129
137
130
138
To add `pause`/`resume`/`cancel` functionality to your `task`, use `configure` to wrap up the original one.
131
139
@@ -142,117 +150,94 @@ configure.cancel = { [weak player] in
142
150
}
143
151
```
144
152
145
-
### task.progress(_ progressClosure:) -> task
153
+
### task.onProgress(_ progressClosure:) -> task
146
154
147
155
```swift
148
-
task.progress { (progressValue: Progress) in
149
-
println(progressValue)
156
+
task.onProgress { (oldValue: Progress?, newValue: Progress) in
157
+
println(newValue)
150
158
return
151
-
}.then { ... }
159
+
}.onSuccess { ... }
152
160
```
153
161
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
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`.
158
163
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*.
166
+
`task.onComplete(completeClosure)` will return a new task where `completeClosure` will be invoked when `task` is either **fulfilled** or **rejected**.
201
167
202
168
This case is similar to JavaScript's `promise.then(onFulfilled, onRejected)`.
// let task2 will be fulfilled with value "\(value!) Swift"
199
+
let task2 = ...
232
200
return task2
233
201
}
234
202
else {
235
203
return someOtherTask
236
204
}
237
-
}.then { (value: String) -> Void in
205
+
}.onSuccess { (value: String) -> Void in
238
206
println("\(value)") // Hello Swift
239
207
return"
240
208
}
241
209
```
242
210
243
-
### task.catch(_ catchClosure:) -> newTask
211
+
### task.onSuccess(_ successClosure:) -> newTask
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**.
214
+
215
+
This case is similar to JavaScript's `promise.then(onFulfilled)`.
216
+
217
+
```swift
218
+
// let task will be fulfilled with value "Hello"
219
+
220
+
task.onSuccess { (value: String) ->Stringin
221
+
return"\(value) World"
222
+
}.onSuccess { (value: String) ->Voidin
223
+
println("\(value)") // Hello World
224
+
return"
225
+
}
226
+
```
227
+
228
+
### task.onFailure(_ failureClosure:) -> newTask
244
229
245
-
Similar to `task.then(fulfilledClosure)` for fulfilled only, `task.catch(catchClosure)` will invoke `catchClosure` only when `task` is either *rejected* or *cancelled*.
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**.
246
231
247
232
This case is similar to JavaScript's `promise.then(undefined, onRejected)` or `promise.catch(onRejected)`.
248
233
249
234
```swift
250
-
// task will be rejected with error "Oh My God"
235
+
// 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
249
265
-
- fulfilled when **all tasks will be fulfilled**
266
-
- rejected when **any of the task will be rejected**
250
+
- fulfilled when **all tasks are fulfilled**
251
+
- rejected when **any of the task is rejected**
267
252
268
253
### Task.any(_ tasks:) -> newTask
269
254
270
255
`Task.any(tasks)` is an opposite of `Task.all(tasks)` which will be:
271
256
272
-
- fulfilled when **any of the task will be fulfilled**
273
-
- rejected when **all tasks will be rejected**
257
+
- fulfilled when **any of the task is fulfilled**
258
+
- rejected when **all tasks are rejected**
274
259
275
260
### Task.some(_ tasks:) -> newTask
276
261
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.
262
+
`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