Skip to content

Commit 1ed0169

Browse files
committed
Update README.md (fix #8 7e93d19)
1 parent 343df7c commit 1ed0169

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

README.md

+30-30
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SwiftTask
1010
- `task.progress()`'s `progressClosure` type changed from `Progress -> Void` to `(oldProgress: Progress?, newProgress: Progress) -> Void`
1111
- `task.then(fulfilledClosure)` is renamed to `task.success()`
1212
- `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)
1414

1515

1616
## Example
@@ -45,10 +45,10 @@ let task = Task<Float, String, NSError> { progress, fulfill, reject, configure i
4545

4646
}
4747

48-
// set onSuccess & onFailure
49-
task.onSuccess { (value: String) -> Void in
48+
// set success & failure
49+
task.success { (value: String) -> Void in
5050
// do something with fulfilled value
51-
}.onFailure { (error: NSError?, isCancelled: Bool) -> Void in
51+
}.failure { (error: NSError?, isCancelled: Bool) -> Void in
5252
// do something with rejected error
5353
}
5454

@@ -96,14 +96,14 @@ let task = AlamoFireTask { progress, fulfill, reject, configure in
9696
return
9797
}
9898

99-
// set onProgress & onComplete
100-
task.onProgress { (oldProgress: Progress?, newProgress: Progress) in
99+
// set progress & then
100+
task.progress { (oldProgress: Progress?, newProgress: Progress) in
101101

102102
println("\(newProgress.bytesWritten)")
103103
println("\(newProgress.totalBytesWritten)")
104104
println("\(newProgress.totalBytesExpectedToWrite)")
105105

106-
}.onComplete { (value: String?, errorInfo: AlamoFireTask.ErrorInfo?) -> Void in
106+
}.then { (value: String?, errorInfo: AlamoFireTask.ErrorInfo?) -> Void in
107107
// do something with fulfilled value or rejected errorInfo
108108
}
109109
```
@@ -131,7 +131,7 @@ let task = Task<Float, NSString?, NSError> { progress, fulfill, reject, configur
131131
}
132132
```
133133

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`.
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`.
135135

136136
Optionally, you can call `progress(progressValue)` multiple times before calling `fulfill`/`reject` to transfer `progressValue` outside of the `initClosure`, notifying it to `task` itself.
137137

@@ -150,50 +150,50 @@ configure.cancel = { [weak player] in
150150
}
151151
```
152152

153-
### task.onProgress(_ progressClosure:) -> task
153+
### task.progress(_ progressClosure:) -> task
154154

155155
```swift
156-
task.onProgress { (oldValue: Progress?, newValue: Progress) in
157-
println(newValue)
156+
task.progress { (oldProgress: Progress?, newProgress: Progress) in
157+
println(newProgress)
158158
return
159-
}.onSuccess { ... }
159+
}.success { ... }
160160
```
161161

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`.
163163

164-
### task.onComplete(_ completeClosure:) -> newTask
164+
### task.then(_ thenClosure:) -> newTask
165165

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**.
167167

168168
This case is similar to JavaScript's `promise.then(onFulfilled, onRejected)`.
169169

170-
`completeClosure` can be two types of closure form:
170+
`thenClosure` can be two types of closure form:
171171

172-
1. `completeClosure: (Value?, ErrorInfo?) -> Value2` (flow: *task => newTask*)
172+
1. `thenClosure: (Value?, ErrorInfo?) -> Value2` (flow: *task => newTask*)
173173

174174
```swift
175175
// let task will be fulfilled with value "Hello"
176176

177-
task.onComplete { (value: String?, errorInfo: ErrorInfo?) -> String in
177+
task.then { (value: String?, errorInfo: ErrorInfo?) -> String in
178178
// nil-check to find out whether task is fulfilled or rejected
179179
if errorInfo == nil {
180180
return "\(value!) World"
181181
}
182182
else {
183183
return "\(value!) Error"
184184
}
185-
}.onSuccess { (value: String) -> Void in
185+
}.success { (value: String) -> Void in
186186
println("\(value)") // Hello World
187187
return"
188188
}
189189
```
190190
191-
2. `completeClosure: (Value?, ErrorInfo?) -> Task` (flow: *task => task2 => newTask*)
191+
2. `thenClosure: (Value?, ErrorInfo?) -> Task` (flow: *task => task2 => newTask*)
192192
193193
```swift
194194
// let task will be fulfilled with value "Hello"
195195
196-
task.onComplete { (value: String?, errorInfo: ErrorInfo?) -> Task<Float, String, NSError> in
196+
task.then { (value: String?, errorInfo: ErrorInfo?) -> Task<Float, String, NSError> in
197197
if errorInfo == nil {
198198
// let task2 will be fulfilled with value "\(value!) Swift"
199199
let task2 = ...
@@ -202,42 +202,42 @@ This case is similar to JavaScript's `promise.then(onFulfilled, onRejected)`.
202202
else {
203203
return someOtherTask
204204
}
205-
}.onSuccess { (value: String) -> Void in
205+
}.success { (value: String) -> Void in
206206
println("\(value)") // Hello Swift
207207
return"
208208
}
209209
```
210210

211-
### task.onSuccess(_ successClosure:) -> newTask
211+
### task.success(_ successClosure:) -> newTask
212212

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**.
214214

215215
This case is similar to JavaScript's `promise.then(onFulfilled)`.
216216

217217
```swift
218218
// let task will be fulfilled with value "Hello"
219219

220-
task.onSuccess { (value: String) -> String in
220+
task.success { (value: String) -> String in
221221
return "\(value) World"
222-
}.onSuccess { (value: String) -> Void in
222+
}.success { (value: String) -> Void in
223223
println("\(value)") // Hello World
224224
return"
225225
}
226226
```
227227
228-
### task.onFailure(_ failureClosure:) -> newTask
228+
### task.failure(_ failureClosure:) -> newTask
229229
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**.
231231
232232
This case is similar to JavaScript's `promise.then(undefined, onRejected)` or `promise.catch(onRejected)`.
233233
234234
```swift
235235
// let task will be rejected with error "Oh My God"
236236
237-
task.onSuccess { (value: String) -> Void in
237+
task.success { (value: String) -> Void in
238238
println("\(value)") // never reaches here
239239
return
240-
}.onFailure { (error: NSError?, isCancelled: Bool) -> Void in
240+
}.failure { (error: NSError?, isCancelled: Bool) -> Void in
241241
println("\(error!)") // Oh My God
242242
return
243243
}

0 commit comments

Comments
 (0)