176
176
* await($promise);
177
177
* ```
178
178
*
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>
181
184
* @since 4.0.0
182
185
* @see coroutine()
183
186
*/
@@ -268,8 +271,9 @@ function async(callable $function): callable
268
271
* }
269
272
* ```
270
273
*
271
- * @param PromiseInterface $promise
272
- * @return mixed returns whatever the promise resolves to
274
+ * @template T
275
+ * @param PromiseInterface<T> $promise
276
+ * @return T
273
277
* @throws \Exception when the promise is rejected with an `Exception`
274
278
* @throws \Throwable when the promise is rejected with a `Throwable`
275
279
* @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
279
283
$ fiber = null ;
280
284
$ resolved = false ;
281
285
$ rejected = false ;
286
+
287
+ /**
288
+ * @var T $resolvedValue
289
+ */
282
290
$ resolvedValue = null ;
283
291
$ rejectedThrowable = null ;
284
292
$ lowLevelFiber = \Fiber::getCurrent ();
@@ -292,6 +300,9 @@ function (mixed $value) use (&$resolved, &$resolvedValue, &$fiber, $lowLevelFibe
292
300
/** @var ?\Fiber<mixed,mixed,mixed,mixed> $fiber */
293
301
if ($ fiber === null ) {
294
302
$ resolved = true ;
303
+ /**
304
+ * @var T $resolvedValue
305
+ */
295
306
$ resolvedValue = $ value ;
296
307
return ;
297
308
}
@@ -305,7 +316,7 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowL
305
316
306
317
if (!$ throwable instanceof \Throwable) {
307
318
$ 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 */
309
320
);
310
321
311
322
// avoid garbage references by replacing all closures in call stack.
@@ -354,7 +365,11 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowL
354
365
355
366
$ fiber = FiberFactory::create ();
356
367
357
- return $ fiber ->suspend ();
368
+ /**
369
+ * @var T $result
370
+ */
371
+ $ result = $ fiber ->suspend ();
372
+ return $ result ;
358
373
}
359
374
360
375
/**
@@ -592,9 +607,10 @@ function delay(float $seconds): void
592
607
* });
593
608
* ```
594
609
*
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
596
612
* @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 >
598
614
* @since 3.0.0
599
615
*/
600
616
function coroutine (callable $ function , mixed ...$ args ): PromiseInterface
@@ -609,9 +625,9 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
609
625
return resolve ($ generator );
610
626
}
611
627
628
+ /** @var ?PromiseInterface<T> $promise */
612
629
$ promise = null ;
613
630
$ deferred = new Deferred (function () use (&$ promise ) {
614
- /** @var ?PromiseInterface $promise */
615
631
if ($ promise instanceof PromiseInterface && \method_exists ($ promise , 'cancel ' )) {
616
632
$ promise ->cancel ();
617
633
}
@@ -632,7 +648,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
632
648
return ;
633
649
}
634
650
635
- /** @var mixed $promise */
651
+ /** @var mixed|PromiseInterface<T> $promise */
636
652
$ promise = $ generator ->current ();
637
653
if (!$ promise instanceof PromiseInterface) {
638
654
$ next = null ;
@@ -660,12 +676,13 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
660
676
}
661
677
662
678
/**
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>>
665
682
*/
666
683
function parallel (iterable $ tasks ): PromiseInterface
667
684
{
668
- /** @var array<int,PromiseInterface> $pending */
685
+ /** @var array<int,PromiseInterface<T> > $pending */
669
686
$ pending = [];
670
687
$ deferred = new Deferred (function () use (&$ pending ) {
671
688
foreach ($ pending as $ promise ) {
@@ -720,14 +737,15 @@ function parallel(iterable $tasks): PromiseInterface
720
737
}
721
738
722
739
/**
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>>
725
743
*/
726
744
function series (iterable $ tasks ): PromiseInterface
727
745
{
728
746
$ pending = null ;
729
747
$ deferred = new Deferred (function () use (&$ pending ) {
730
- /** @var ?PromiseInterface $pending */
748
+ /** @var ?PromiseInterface<T> $pending */
731
749
if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
732
750
$ pending ->cancel ();
733
751
}
@@ -774,14 +792,15 @@ function series(iterable $tasks): PromiseInterface
774
792
}
775
793
776
794
/**
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>
779
798
*/
780
799
function waterfall (iterable $ tasks ): PromiseInterface
781
800
{
782
801
$ pending = null ;
783
802
$ deferred = new Deferred (function () use (&$ pending ) {
784
- /** @var ?PromiseInterface $pending */
803
+ /** @var ?PromiseInterface<T> $pending */
785
804
if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
786
805
$ pending ->cancel ();
787
806
}
0 commit comments