Example #1
0
 /**
  * {@inheritdoc}
  */
 public function then(callable $onFulfilled = null, callable $onRejected = null) : Awaitable
 {
     if (null !== $this->result) {
         return $this->unwrap()->then($onFulfilled, $onRejected);
     }
     if (null === $onFulfilled && null === $onRejected) {
         return $this;
     }
     ++$this->children;
     $future = new self(function (Throwable $exception) {
         if (0 === --$this->children) {
             $this->cancel($exception);
         }
     });
     if (null !== $onFulfilled) {
         $onFulfilled = function ($value) use($future, $onFulfilled) {
             try {
                 $future->resolve($onFulfilled($value));
             } catch (Throwable $exception) {
                 $future->reject($exception);
             }
         };
     } else {
         $onFulfilled = function () use($future) {
             $future->resolve($this->result);
         };
     }
     if (null !== $onRejected) {
         $onRejected = function (Throwable $exception) use($future, $onRejected) {
             try {
                 $future->resolve($onRejected($exception));
             } catch (Throwable $exception) {
                 $future->reject($exception);
             }
         };
     } else {
         $onRejected = function () use($future) {
             $future->resolve($this->result);
         };
     }
     $this->done($onFulfilled, $onRejected);
     return $future;
 }
Example #2
0
 public function range($stop = null)
 {
     $args = self::_wrapArgs(func_get_args(), 1);
     $__ = new self();
     $args = $__->reject($args, function ($val) {
         return is_null($val);
     });
     $num_args = count($args);
     switch ($num_args) {
         case 1:
             list($start, $stop, $step) = array(0, $args[0], 1);
             break;
         case 2:
             list($start, $stop, $step) = array($args[0], $args[1], 1);
             if ($stop < $start) {
                 return self::_wrap(array());
             }
             break;
         default:
             list($start, $stop, $step) = array($args[0], $args[1], $args[2]);
             if ($step > 0 && $step > $stop) {
                 return self::_wrap(array($start));
             }
     }
     $results = range($start, $stop, $step);
     // Switch inclusive to exclusive
     if ($step > 0 && $__->last($results) >= $stop) {
         array_pop($results);
     } elseif ($step < 0 && $__->last($results) <= $stop) {
         array_pop($results);
     }
     return self::_wrap($results);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function timeout(float $timeout, callable $onTimeout = null) : Awaitable
 {
     if (null !== $this->result) {
         return $this->unwrap()->timeout($timeout, $onTimeout);
     }
     ++$this->children;
     $future = new self(function (Throwable $exception) {
         if (0 === --$this->children) {
             $this->cancel($exception);
         }
     });
     $timer = Loop\timer($timeout, function () use($future, $onTimeout) {
         if (null === $onTimeout) {
             $future->reject(new TimeoutException());
             return;
         }
         try {
             $future->resolve($onTimeout());
         } catch (Throwable $exception) {
             $future->reject($exception);
         }
     });
     $onResolved = function () use($future, $timer) {
         $future->resolve($this->result);
         $timer->stop();
     };
     $this->done($onResolved, $onResolved);
     return $future;
 }