public static function resolve($value, Promise $promise = null) { if ($value === $promise) { throw new \InvalidArgumentException('$promise === $value'); } if ($value instanceof Promise) { return $value; } else { if ($promise === null) { $promise = new Promise(); } if (is_object($value) && method_exists($value, 'then')) { try { $value->then(function ($value) use($promise) { Promise::resolve($value, $promise); }, [$promise, 'reject']); } catch (\Exception $e) { if ($promise->getState() === 'pending') { $promise->reject($e); } } } else { $promise->fulfill($value); } return $promise; } }
protected function postResolveTask(Promise &$child, callable $callback, $value) { $this->tasks->enqueue(function () use(&$child, $callback, $value) { try { $child = Promise::resolve($callback($value), $child); } catch (\Exception $e) { $child->reject($e); } }); }