コード例 #1
0
 /**
  * @param Psr7Request $request
  * @param array       $options
  *
  * @return Promise\Promise
  */
 public function __invoke(Psr7Request $request, array $options = [])
 {
     // Create and send a Guzzle 5 request
     $guzzlePromise = $this->client->send($this->createGuzzleRequest($request, $options));
     $promise = new Promise\Promise(function () use($guzzlePromise) {
         try {
             $guzzlePromise->wait();
         } catch (\Exception $e) {
             // The promise is already delivered when the exception is
             // thrown, so don't rethrow it.
         }
     }, [$guzzlePromise, 'cancel']);
     $guzzlePromise->then([$promise, 'resolve'], [$promise, 'reject']);
     return $promise->then(function (GuzzleResponse $response) {
         // Adapt the Guzzle 5 Future to a Guzzle 6 ResponsePromise.
         return $this->createPsr7Response($response);
     }, function (Exception $exception) {
         // Reject with information about the error.
         return new Promise\RejectedPromise($this->prepareErrorData($exception));
     });
 }
コード例 #2
0
ファイル: Request.php プロジェクト: ferjul17/Sellsy-api
 /**
  * @param string $method
  * @param mixed  $params
  *
  * @return PromiseInterface
  */
 public function callAsync($method, $params)
 {
     $requestId = uniqid();
     $options = $this->prepareCall($method, $params, $requestId);
     $response = $this->getClient()->postAsync('', $options);
     $promise = new Promise(function ($unwrap) use($response) {
         return $response->wait($unwrap);
     }, function () use($response) {
         return $response->cancel();
     });
     $response->then(function (ResponseInterface $res) use($promise) {
         try {
             $promise->resolve($this->handleResponse($res->getBody()));
             return $res;
         } catch (\Exception $e) {
             $promise->reject($e);
             throw $e;
         }
     }, function (\Exception $reqException) use($promise, $requestId) {
         $this->logResponse($requestId, strval($reqException));
         $promise->reject($reqException);
         return $reqException;
     });
     $promise->then(function ($res) use($requestId) {
         $this->logResponse($requestId, json_encode($res));
         return $res;
     }, function ($res) use($requestId) {
         $this->logResponse($requestId, strval($res));
         return $res;
     });
     return $promise;
 }
コード例 #3
0
ファイル: PromiseTest.php プロジェクト: lewishealey/confetti
 public function testRecursivelyForwardsWhenNotInstanceOfPromise()
 {
     $res = [];
     $p = new Promise();
     $p2 = new NotPromiseInstance();
     $p2->then(function ($v) use(&$res) {
         $res[] = 'A:' . $v;
     });
     $p->then(function () use($p2, &$res) {
         $res[] = 'B';
         return $p2;
     })->then(function ($v) use(&$res) {
         $res[] = 'C:' . $v;
     });
     $p->resolve('a');
     $p->then(function ($v) use(&$res) {
         $res[] = 'D:' . $v;
     });
     P\queue()->run();
     $this->assertEquals(['B', 'D:a'], $res);
     $p2->resolve('foo');
     P\queue()->run();
     $this->assertEquals(['B', 'D:a', 'A:foo', 'C:foo'], $res);
 }
コード例 #4
0
 /**
  * @param Promise $req
  *
  * @return Promise Promise with attached transformers
  */
 private function attachTransforms(Promise $req)
 {
     $transformers = $this->getTransformers();
     return $req->then(function (Response $res) use($transformers) {
         $data = \json_decode((string) $res->getBody());
         if (is_object($data)) {
             foreach ($transformers as $transformer) {
                 $data = \call_user_func($transformer, $data);
             }
         } elseif (is_array($data)) {
             foreach ($data as $key => $val) {
                 foreach ($transformers as $transformer) {
                     $data[$key] = \call_user_func($transformer, $val);
                 }
             }
         }
         return $data;
     }, function (RequestException $e) {
     });
 }
コード例 #5
0
ファイル: Coroutine.php プロジェクト: nsandlin/linepig
 public function then(callable $onFulfilled = null, callable $onRejected = null)
 {
     return $this->result->then($onFulfilled, $onRejected);
 }