-
-
Notifications
You must be signed in to change notification settings - Fork 2
Getting started
Greg Bowler edited this page May 4, 2026
·
1 revision
Before we can use this package, we need to install it with Composer and create a Deferred that exposes a Promise.
composer require phpgt/promiseThe smallest useful setup is:
use GT\Promise\Deferred;
$deferred = new Deferred(function() use (&$deferred) {
$deferred->resolve("Finished");
});
$promise = $deferred->getPromise();
$promise->then(function(string $message) {
echo $message, PHP_EOL;
});
foreach($deferred->getProcessList() as $process) {
$process();
}Here is what happens:
- We construct a
Deferred. - The
Deferredcreates and keeps a matchingPromise. - We attach a
then()callback to the promise. - We run the deferred process callback.
- The process resolves the promise, so the
then()callback runs.
-
DeferredandPromiseare separate objects with separate jobs. - The package does not run process callbacks for us.
-
then(),catch(), andfinally()can be attached before or after resolution. - A promise stays pending until something calls
resolve()orreject().
If you are coming from JavaScript, the syntax will look familiar, but the scheduling is more explicit because PHP does not provide an event loop by default.
Next, learn the difference between Deferred and Promise objects.
PHP.GT/Promise is a separately maintained component of PHP.GT/WebEngine.