| | 1 | === Notes for an API to retrieve the result of a Deferred synchronously === |
| | 2 | |
| | 3 | `resultNow` is a function which accepts a `Deferred` and returns the result of the `Deferred`. It is an error if the `Deferred` does not have a result yet. It is intended to simplify unit tests for cases where a `Deferred` is required to have a result at a particular time. |
| | 4 | |
| | 5 | Use cases: |
| | 6 | |
| | 7 | The latter half of |
| | 8 | {{{ |
| | 9 | def test_synchronousImplicitChain(self): |
| | 10 | """ |
| | 11 | If a first L{Deferred} with a result is returned from a callback on a |
| | 12 | second L{Deferred}, the result of the second L{Deferred} becomes the |
| | 13 | result of the first L{Deferred} and the result of the first L{Deferred} |
| | 14 | becomes C{None}. |
| | 15 | """ |
| | 16 | result = object() |
| | 17 | first = defer.succeed(result) |
| | 18 | second = defer.Deferred() |
| | 19 | second.addCallback(lambda ign: first) |
| | 20 | second.callback(None) |
| | 21 | |
| | 22 | results = [] |
| | 23 | first.addCallback(results.append) |
| | 24 | self.assertIdentical(results[0], None) |
| | 25 | second.addCallback(results.append) |
| | 26 | self.assertIdentical(results[1], result) |
| | 27 | }}} |
| | 28 | |
| | 29 | could be rewritten more simply: |
| | 30 | |
| | 31 | {{{ |
| | 32 | self.assertIdentical(resultNow(first), None) |
| | 33 | self.assertIdentical(resultNow(second), result) |
| | 34 | }}} |
| | 35 | |