예제 #1
0
파일: LazyPromise.php 프로젝트: amphp/amp
 /**
  * {@inheritdoc}
  */
 public function when(callable $onResolved)
 {
     if (null === $this->promise) {
         $provider = $this->provider;
         $this->provider = null;
         try {
             $this->promise = $provider();
             if (!$this->promise instanceof Promise) {
                 $this->promise = new Success($this->promise);
             }
         } catch (\Throwable $exception) {
             $this->promise = new Failure($exception);
         }
     }
     $this->promise->when($onResolved);
 }
예제 #2
0
파일: Timeout.php 프로젝트: koolkode/async
 public function __construct(float $seconds, Promise $promise)
 {
     $this->promise = $promise;
     $promise->when(function (\Throwable $e = null, ...$val) {
         if ($this->state === self::PENDING) {
             Loop::cancel($this->watcher);
             if ($e) {
                 $this->fail($e);
             } else {
                 $this->resolve(...$val);
             }
         }
     });
     if ($this->state === self::PENDING) {
         $this->watcher = Loop::delay((int) \floor($seconds * 1000), function () use($seconds) {
             $this->watcher = null;
             if ($this->state === self::PENDING) {
                 $e = new TimeoutException(\sprintf('Operation timed out after %.3f seconds', $seconds));
                 if ($this->promise instanceof Awaitable) {
                     $this->state = self::FAILED;
                     try {
                         $this->promise->cancel($e);
                     } finally {
                         $this->state = self::PENDING;
                     }
                 }
                 $this->fail($e);
             }
         });
     }
 }
예제 #3
0
파일: Task.php 프로젝트: koolkode/async
 public function __construct(Promise $promise, CancellationTokenSource $source = null)
 {
     $this->promise = $promise;
     $this->source = $source;
     $promise->when(function (\Throwable $e = null, ...$val) {
         if ($this->state === self::PENDING) {
             if ($e) {
                 $this->fail($e);
             } else {
                 $this->resolve(...$val);
             }
         }
     });
 }
예제 #4
0
 public function __construct(Promise $promise, callable $callback, bool $expand = false)
 {
     $this->promise = $promise;
     $promise->when(function (\Throwable $error = null, ...$val) use($callback, $expand) {
         if ($this->state === self::PENDING) {
             if ($error) {
                 $this->fail($error);
             } else {
                 if ($expand) {
                     $this->resolve($callback(...$val[0]));
                 } else {
                     $this->resolve($callback(...$val));
                 }
             }
         }
     });
 }