public function testPromiseThen__ThenRejectedByPromiseButResolved() { $result = new Client($this->store); $promise1 = new Client($this->store); $this->object = $promise1->then(null, function ($reason) { return $reason->getMessage() . ' was resolved'; }); $promise1->reject($result); $this->assertEquals($result->getId() . ' was resolved', $this->object->wait(false)); $this->assertEquals(PromiseInterface::FULFILLED, $this->object->getState()); }
/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @return ResponseInterface * @throws \zaboy\rest\RestException * @internal param callable|null $next */ public function methodPutWithId(ServerRequestInterface $request, ResponseInterface $response) { $primaryId = $request->getAttribute('Primary-Key-Value'); if (!$this->isId($primaryId)) { throw new PromiseException('There is no promise. PromiseId: ' . $primaryId); } $promise = new Client($this->store, $primaryId); $promiseData = $request->getParsedBody(); if (!isset($promiseData[Store::STATE])) { throw new PromiseException('There is no key STATE in the Body. PromiseId: ' . $primaryId); } if (!isset($promiseData[Store::RESULT])) { throw new PromiseException('There is no key RESULT in the Body. PromiseId: ' . $primaryId); } switch ($promiseData[Store::STATE]) { case PromiseInterface::FULFILLED: $promise->resolve($promiseData[Store::RESULT]); break; case PromiseInterface::REJECTED: $promise->reject($promiseData[Store::RESULT]); break; default: throw new PromiseException('The STATE field must be FULFILLED or REJECTED. PromiseId: ' . $primaryId); } $responseBody = $promise->toArray(); $this->request = $request->withAttribute('Response-Body', $responseBody); $response = $response->withStatus(200); return $response; }
use zaboy\scheduler\FileSystem\CommandLineWorker; use zaboy\scheduler\Callback\CallbackException; use zaboy\async\Promise\Store; use zaboy\async\Promise\Client as Promise; $commandLineWorker = new CommandLineWorker(); $options = $commandLineWorker->getCallOptions($_SERVER['argv']); if (!isset($options['rpc_callback'])) { throw new CallbackException("The necessary parameter \"rpc_callback\" does not exist"); } $callbackServiceName = $options['rpc_callback']; unset($options['rpc_callback']); /** @var Zend\ServiceManager\ServiceManager $container */ $container = (include './config/container.php'); /** @var zaboy\scheduler\Callback\CallbackManager $callbackManager */ $callbackManager = $container->get('callback_manager'); /** @var Store $store */ $store = $container->get('Store'); $promise = new Promise($store, $options['promise']); unset($options['promise']); try { if (is_callable($callbackServiceName)) { $result = call_user_func($callbackServiceName, $options); } elseif ($callbackManager->has($callbackServiceName)) { $result = $callbackManager->{$callbackServiceName}($options); } else { throw new CallbackException('Specified callback "' . print_r($callbackServiceName) . '" wasn\'t found'); } $promise->resolve($result); } catch (\Exception $e) { $promise->reject($e); }