Version 2 (modified by Tom Prince, 4 years ago) (diff)

Update to refer to existing solution.

Notes for an API to retrieve the result of a Deferred synchronously

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.

We now have a way of doing this, that is accessible on TestCase.

Use cases:

The latter half of

    def test_synchronousImplicitChain(self):
        If a first L{Deferred} with a result is returned from a callback on a
        second L{Deferred}, the result of the second L{Deferred} becomes the
        result of the first L{Deferred} and the result of the first L{Deferred}
        becomes C{None}.
        result = object()
        first = defer.succeed(result)
        second = defer.Deferred()
        second.addCallback(lambda ign: first)

        results = []
        self.assertIdentical(results[0], None)
        self.assertIdentical(results[1], result)

can be rewritten more simply:

        self.assertIdentical(self.successResultNow(first), None)
        self.assertIdentical(self.succeesResultNow(second), result)