@@ -38,9 +38,10 @@ types will be referred to by name.
3838 1 . [ Concatenating] ( #concatenating )
3939 1 . [ Switching to the latest] ( #switching-to-the-latest )
4040
41- ** [ Handling failures ] ( #handling-failures ) **
41+ ** [ Working with errors ] ( #working-with-errors ) **
4242
4343 1 . [ Catching failures] ( #catching-failures )
44+ 1 . [ Failable transformations] ( #failable-transformations )
4445 1 . [ Retrying] ( #retrying )
4546 1 . [ Mapping errors] ( #mapping-errors )
4647 1 . [ Promote] ( #promote )
@@ -370,9 +371,9 @@ lettersObserver.send(value: "c") // nothing printed
370371numbersObserver.send (value : " 3" ) // prints "3"
371372```
372373
373- ## Handling failures
374+ ## Working with errors
374375
375- These operators are used to handle failures that might occur on an event stream.
376+ These operators are used to handle failures that might occur on an event stream, or perform operations that might fail on an event stream .
376377
377378### Catching failures
378379
@@ -394,6 +395,32 @@ observer.send(value: "Second") // prints "Second"
394395observer.send (error : error) // prints "Default"
395396```
396397
398+ ### Failable transformations
399+
400+ ` SignalProducer.attempt(_:) ` allows you to turn a failable operation into an event stream.
401+ The ` attempt(_:) ` and ` attemptMap(_:) ` operators allow you to perform failable operations or transformations on an event stream.
402+
403+ ``` swift
404+ let dictionaryPath = URL (fileURLWithPath : " /usr/share/dict/words" )
405+
406+ // Create a `SignalProducer` that lazily attempts the closure
407+ // whenever it is started
408+ let data = SignalProducer.attempt { try Data (contentsOf : dictionaryPath) }
409+
410+ // Lazily apply a failable transformation
411+ let json = data.attemptMap { try JSONSerialization.jsonObject (with : $0 ) }
412+
413+ json.startWithResult { result in
414+ switch result {
415+ case let .success (words):
416+ print (" Dictionary as JSON:" )
417+ print (words)
418+ case let .failure (error):
419+ print (" Couldn't parse dictionary as JSON: \( error ) " )
420+ }
421+ }
422+ ```
423+
397424### Retrying
398425
399426The ` retry ` operator will restart the original ` SignalProducer ` on failure up to ` count ` times.
0 commit comments