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
}
@@ -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
/**
@@ -609,9 +624,9 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
609
624
return resolve ($ generator );
610
625
}
611
626
627
+ /** @var ?PromiseInterface<mixed> $promise */
612
628
$ promise = null ;
613
629
$ deferred = new Deferred (function () use (&$ promise ) {
614
- /** @var ?PromiseInterface $promise */
615
630
if ($ promise instanceof PromiseInterface && \method_exists ($ promise , 'cancel ' )) {
616
631
$ promise ->cancel ();
617
632
}
@@ -660,12 +675,13 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
660
675
}
661
676
662
677
/**
663
- * @param iterable<callable():PromiseInterface<mixed>> $tasks
664
- * @return PromiseInterface<array<mixed>>
678
+ * @template T
679
+ * @param iterable<callable():PromiseInterface<T>> $tasks
680
+ * @return PromiseInterface<array<T>>
665
681
*/
666
682
function parallel (iterable $ tasks ): PromiseInterface
667
683
{
668
- /** @var array<int,PromiseInterface> $pending */
684
+ /** @var array<int,PromiseInterface<T> > $pending */
669
685
$ pending = [];
670
686
$ deferred = new Deferred (function () use (&$ pending ) {
671
687
foreach ($ pending as $ promise ) {
@@ -720,14 +736,15 @@ function parallel(iterable $tasks): PromiseInterface
720
736
}
721
737
722
738
/**
723
- * @param iterable<callable():PromiseInterface<mixed>> $tasks
724
- * @return PromiseInterface<array<mixed>>
739
+ * @template T
740
+ * @param iterable<callable():PromiseInterface<T>> $tasks
741
+ * @return PromiseInterface<array<T>>
725
742
*/
726
743
function series (iterable $ tasks ): PromiseInterface
727
744
{
728
745
$ pending = null ;
729
746
$ deferred = new Deferred (function () use (&$ pending ) {
730
- /** @var ?PromiseInterface $pending */
747
+ /** @var ?PromiseInterface<T> $pending */
731
748
if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
732
749
$ pending ->cancel ();
733
750
}
@@ -774,14 +791,15 @@ function series(iterable $tasks): PromiseInterface
774
791
}
775
792
776
793
/**
777
- * @param iterable<(callable():PromiseInterface<mixed>)|(callable(mixed):PromiseInterface<mixed>)> $tasks
778
- * @return PromiseInterface<mixed>
794
+ * @template T
795
+ * @param iterable<(callable():PromiseInterface<T>)|(callable(mixed):PromiseInterface<T>)> $tasks
796
+ * @return PromiseInterface<T>
779
797
*/
780
798
function waterfall (iterable $ tasks ): PromiseInterface
781
799
{
782
800
$ pending = null ;
783
801
$ deferred = new Deferred (function () use (&$ pending ) {
784
- /** @var ?PromiseInterface $pending */
802
+ /** @var ?PromiseInterface<T> $pending */
785
803
if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
786
804
$ pending ->cancel ();
787
805
}
0 commit comments