jkimbo / graphql-sync-dataloaders

Use DataLoaders in your Python GraphQL servers that have to run in a sync context (i.e. Django).
MIT License
41 stars 21 forks source link

Calling `SyncFuture.then()` on a "finished" Future doesn't work #20

Open bvallant opened 9 months ago

bvallant commented 9 months ago

Calling then() only has an effect if the SyncFuture is still pending. Should probably behave more like this:


    def then(self, on_complete: Callable) -> "SyncFuture":
        ret = SyncFuture()

        def call_and_resolve(v: Any) -> None:
            try:
                ret.set_result(on_complete(v))
            except Exception as e:
                ret.set_exception(e)

        if self._state == _FINISHED:
            call_and_resolve(self.result())
        else:
            self.add_done_callback(call_and_resolve)

        return ret