Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

### Added

- Backward-compatible generic annotations for resolved values.

## 1.3.1 - 2024-03-15

- Made nullable parameter types explicit (PHP 8.4 compatibility)
Expand Down
11 changes: 9 additions & 2 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@
* A promise already fulfilled.
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*
* @template-covariant T = mixed
*
* @implements Promise<T>
*/
final class FulfilledPromise implements Promise
{
/**
* @var mixed
* @var T
*/
private $result;

/**
* @param mixed $result
* @param T $result
*/
public function __construct($result)
{
$this->result = $result;
}

/**
* @return ($onFulfilled is null ? self<T> : Promise<mixed>)
*/
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
{
if (null === $onFulfilled) {
Expand Down
6 changes: 4 additions & 2 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*
* @template-covariant T = mixed
*/
interface Promise
{
Expand Down Expand Up @@ -39,7 +41,7 @@ interface Promise
* @param callable|null $onFulfilled called when a response will be available
* @param callable|null $onRejected called when an exception occurs
*
* @return Promise a new resolved promise with value of the executed callback (onFulfilled / onRejected)
* @return Promise<mixed> a new resolved promise with value of the executed callback (onFulfilled / onRejected)
*/
public function then(?callable $onFulfilled = null, ?callable $onRejected = null);

Expand All @@ -61,7 +63,7 @@ public function getState();
*
* @param bool $unwrap Whether to return resolved value / throw reason or not
*
* @return ($unwrap is true ? mixed : null) Resolved value, null if $unwrap is set to false
* @return ($unwrap is true ? T : null) Resolved value, null if $unwrap is set to false
*
* @throws \Throwable the rejection reason if $unwrap is set to true and the request failed
*/
Expand Down
5 changes: 5 additions & 0 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* A rejected promise.
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*
* @implements Promise<never>
*/
final class RejectedPromise implements Promise
{
Expand All @@ -19,6 +21,9 @@ public function __construct(\Throwable $exception)
$this->exception = $exception;
}

/**
* @return ($onRejected is null ? self : Promise<mixed>)
*/
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
{
if (null === $onRejected) {
Expand Down
Loading