then() & catch() & finally()

/**
 * This method is used to set the callback when the promise is resolved.
 */
public function then(callable $callback): Promise;

/**
 * This method is used to set the callback when the promise is rejected.
 */
public function catch(callable $callback): Promise;

/**
 * This method is used to set the callback when the promise is resolved or rejected.
 */
public function finally(callable $callback): Promise;

The then() method will help you create that promise callback when it has been fulfilled.

The catch() method will generate a callback for that promise when it is rejected.

finally() is the method used to set that callback when it's complete.

Example:

$promise = new Promise(function($resolve, $reject) {
    $resolve("Hello World");
});

$promise->then(fn($result) => var_dump($result))
->catch(fn($error) => var_dump($error))
->finnally(fn() => var_dump("PROMISE DONE!"));

Last updated