Async documentation
As PHP is inherently synchronous, running code asynchronously requires a background loop that observes and dispatches events while handling promise resolutions. PHP.GT/Async introduces an event loop, timers and a promise-based approach to handling deferred operations.
This package provides:
Loop, which waits until work is due and then dispatches itIndividualTimer, for one-off trigger timesPeriodicTimer, for repeated trigger timesEventandEventDispatcher, for lightweight publish-subscribe messaging- Loop support for tracking
Deferredobjects fromphpgt/promise
The package is standalone. We can use it in any PHP project with Composer. WebEngine also depends on this package, but the classes documented here can still be used directly outside WebEngine.
A minimal example
use Gt\Async\Loop;
use Gt\Async\Timer\IndividualTimer;
$loop = new Loop();
$timer = new IndividualTimer(2);
$timer->addCallback(function() use($loop) {
echo "Two seconds have passed.", PHP_EOL;
$loop->halt();
});
$loop->addTimer($timer);
$loop->run();
Here we schedule one callback to run two seconds in the future. Once it has run, the callback halts the loop.
Start with Getting started to build a working loop from scratch.