176176 * await($promise);
177177 * ```
178178 *
179- * @param callable $function
180- * @return callable(mixed ...): PromiseInterface<mixed>
179+ * @template T
180+ * @template TFulfilled as PromiseInterface<T>|T
181+ * @template A
182+ * @param (callable(): TFulfilled)|(callable(A): TFulfilled) $function
183+ * @return callable(mixed ...$args): PromiseInterface<T>
181184 * @since 4.0.0
182185 * @see coroutine()
183186 */
@@ -268,8 +271,9 @@ function async(callable $function): callable
268271 * }
269272 * ```
270273 *
271- * @param PromiseInterface $promise
272- * @return mixed returns whatever the promise resolves to
274+ * @template T
275+ * @param PromiseInterface<T> $promise
276+ * @return T
273277 * @throws \Exception when the promise is rejected with an `Exception`
274278 * @throws \Throwable when the promise is rejected with a `Throwable`
275279 * @throws \UnexpectedValueException when the promise is rejected with an unexpected value (Promise API v1 or v2 only)
@@ -279,6 +283,10 @@ function await(PromiseInterface $promise): mixed
279283 $ fiber = null ;
280284 $ resolved = false ;
281285 $ rejected = false ;
286+
287+ /**
288+ * @var T $resolvedValue
289+ */
282290 $ resolvedValue = null ;
283291 $ rejectedThrowable = null ;
284292 $ lowLevelFiber = \Fiber::getCurrent ();
@@ -292,6 +300,9 @@ function (mixed $value) use (&$resolved, &$resolvedValue, &$fiber, $lowLevelFibe
292300 /** @var ?\Fiber<mixed,mixed,mixed,mixed> $fiber */
293301 if ($ fiber === null ) {
294302 $ resolved = true ;
303+ /**
304+ * @var T $resolvedValue
305+ */
295306 $ resolvedValue = $ value ;
296307 return ;
297308 }
@@ -305,7 +316,7 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowL
305316
306317 if (!$ throwable instanceof \Throwable) {
307318 $ throwable = new \UnexpectedValueException (
308- 'Promise rejected with unexpected value of type ' . (is_object ($ throwable ) ? get_class ($ throwable ) : gettype ($ throwable ))
319+ 'Promise rejected with unexpected value of type ' . (is_object ($ throwable ) ? get_class ($ throwable ) : gettype ($ throwable )) /** @phpstan-ignore-line */
309320 );
310321
311322 // avoid garbage references by replacing all closures in call stack.
@@ -354,7 +365,11 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowL
354365
355366 $ fiber = FiberFactory::create ();
356367
357- return $ fiber ->suspend ();
368+ /**
369+ * @var T $result
370+ */
371+ $ result = $ fiber ->suspend ();
372+ return $ result ;
358373}
359374
360375/**
@@ -592,9 +607,10 @@ function delay(float $seconds): void
592607 * });
593608 * ```
594609 *
595- * @param callable(mixed ...$args):(\Generator<mixed,PromiseInterface,mixed,mixed>|mixed) $function
610+ * @template T
611+ * @param callable(mixed ...$args):(\Generator<mixed,PromiseInterface<T>,mixed,mixed>|T) $function
596612 * @param mixed ...$args Optional list of additional arguments that will be passed to the given `$function` as is
597- * @return PromiseInterface<mixed >
613+ * @return PromiseInterface<T >
598614 * @since 3.0.0
599615 */
600616function coroutine (callable $ function , mixed ...$ args ): PromiseInterface
@@ -609,9 +625,9 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
609625 return resolve ($ generator );
610626 }
611627
628+ /** @var ?PromiseInterface<T> $promise */
612629 $ promise = null ;
613630 $ deferred = new Deferred (function () use (&$ promise ) {
614- /** @var ?PromiseInterface $promise */
615631 if ($ promise instanceof PromiseInterface && \method_exists ($ promise , 'cancel ' )) {
616632 $ promise ->cancel ();
617633 }
@@ -632,7 +648,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
632648 return ;
633649 }
634650
635- /** @var mixed $promise */
651+ /** @var mixed|PromiseInterface<T> $promise */
636652 $ promise = $ generator ->current ();
637653 if (!$ promise instanceof PromiseInterface) {
638654 $ next = null ;
@@ -660,12 +676,13 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
660676}
661677
662678/**
663- * @param iterable<callable():PromiseInterface<mixed>> $tasks
664- * @return PromiseInterface<array<mixed>>
679+ * @template T
680+ * @param iterable<callable():PromiseInterface<T>> $tasks
681+ * @return PromiseInterface<array<T>>
665682 */
666683function parallel (iterable $ tasks ): PromiseInterface
667684{
668- /** @var array<int,PromiseInterface> $pending */
685+ /** @var array<int,PromiseInterface<T> > $pending */
669686 $ pending = [];
670687 $ deferred = new Deferred (function () use (&$ pending ) {
671688 foreach ($ pending as $ promise ) {
@@ -720,14 +737,15 @@ function parallel(iterable $tasks): PromiseInterface
720737}
721738
722739/**
723- * @param iterable<callable():PromiseInterface<mixed>> $tasks
724- * @return PromiseInterface<array<mixed>>
740+ * @template T
741+ * @param iterable<callable():PromiseInterface<T>> $tasks
742+ * @return PromiseInterface<array<T>>
725743 */
726744function series (iterable $ tasks ): PromiseInterface
727745{
728746 $ pending = null ;
729747 $ deferred = new Deferred (function () use (&$ pending ) {
730- /** @var ?PromiseInterface $pending */
748+ /** @var ?PromiseInterface<T> $pending */
731749 if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
732750 $ pending ->cancel ();
733751 }
@@ -774,14 +792,15 @@ function series(iterable $tasks): PromiseInterface
774792}
775793
776794/**
777- * @param iterable<(callable():PromiseInterface<mixed>)|(callable(mixed):PromiseInterface<mixed>)> $tasks
778- * @return PromiseInterface<mixed>
795+ * @template T
796+ * @param iterable<(callable():PromiseInterface<T>)|(callable(mixed):PromiseInterface<T>)> $tasks
797+ * @return PromiseInterface<T>
779798 */
780799function waterfall (iterable $ tasks ): PromiseInterface
781800{
782801 $ pending = null ;
783802 $ deferred = new Deferred (function () use (&$ pending ) {
784- /** @var ?PromiseInterface $pending */
803+ /** @var ?PromiseInterface<T> $pending */
785804 if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
786805 $ pending ->cancel ();
787806 }
0 commit comments