azjezz/psl
PSL is a modern, well-typed standard library for PHP 8.4+, inspired by HHVM’s HSL. It offers safer, predictable APIs for async, collections, networking, I/O, crypto, terminal UI, and robust data validation—replacing brittle built-ins with consistent alternatives.
The Promise component provides a promise interface for deferred computations. A promise represents a value that will be available in the future -- either resolved with a success value or rejected with an exception. You attach callbacks to react to the outcome without blocking.
PromiseInterface<T> defines four methods for composing asynchronous operations:
then() -- Handle both success and failure in one callmap() -- Transform the resolved valuecatch() -- Recover from rejectionalways() -- Run cleanup logic regardless of outcomeEvery method returns a new PromiseInterface, so you can chain them into pipelines.
map() applies a function to the resolved value. If the promise is rejected, the callback is skipped and the rejection propagates:
@example('async/promise-map.php')
catch() attaches a callback that is only invoked when the promise is rejected. The returned value becomes the new resolved value:
@example('async/promise-catch.php')
then() is a shortcut for chaining map() and catch():
@example('async/promise-then.php')
This is equivalent to:
@example('async/promise-then-equivalent.php')
always() runs a callback when the promise settles, regardless of whether it resolved or was rejected. The promise's value passes through unchanged (unless the callback throws):
@example('async/promise-always.php')
Because every method returns a new promise, you can compose multi-step async workflows:
@example('async/promise-pipeline.php')
always() to guarantee cleanup runsSee src/Psl/Promise/ for the full API.
How can I help you explore Laravel packages today?