- function __invoke(callable $callback) {
- if (empty($this->args["fresh"]) && ($cache = $this->api->getCacheStorage())) {
- $key = $this->getCacheKey();
-
- if ($cache->get($key, $cached)) {
- call_user_func_array($callback, $cached);
- return $this->api->getClient();
- }
+ /**
+ * @return \React\Promise\Promise
+ */
+ function __invoke() {
+ if ($this->readFromCache($this->result)) {
+ return new Promise\FulfilledPromise($this->result);
+ } else {
+ $this->api->getClient()->enqueue(
+ $this->request(),
+ function($response) {
+ try {
+ $result = $this->response($response);
+ if (!($result instanceof Promise\PromiseInterface)) {
+ $this->deferred->resolve($result);
+ }
+ } catch (\Exception $e) {
+ $this->deferred->reject($e);
+ }
+ return true;
+ }
+ );
+ return $this->deferred->promise();