-
-
Notifications
You must be signed in to change notification settings - Fork 2
Async event loop
This package does not provide an event loop. That part is left to the calling application.
Without something repeatedly calling each deferred process callback, a pending promise will stay pending forever.
If we only need a small proof of concept, we can loop over active deferred tasks ourselves:
/** @var GT\Promise\Deferred[] $deferredList */
while($deferredList) {
foreach($deferredList as $index => $deferred) {
foreach($deferred->getProcessList() as $process) {
$process();
}
if(!$deferred->isActive()) {
unset($deferredList[$index]);
}
}
}The important part is simple:
- read each deferred's process list
- call each process callback
- stop tracking the deferred when
isActive()becomesfalse
For real applications, you will usually want a dedicated loop implementation. Within PHP.GT, that role is handled by PHP.GT/Async.
use Gt\Async\Loop;
use Gt\Async\PeriodicTimer;
$timer = new PeriodicTimer(0.1, true);
$loop = new Loop();
$loop->addTimer($timer);
/** @var GT\Promise\Deferred $deferred */
$loop->addDeferredToTimer($deferred);
$loop->run();This keeps Promise focused on representing work and results, while Async focuses on scheduling.
When Promise is used inside a wider PHP.GT application, it is usually through a higher-level package that already knows how to schedule its own deferred work. This repository still remains a standalone package, so the examples in this guide stay at that lower level.
PHP.GT/Promise is a separately maintained component of PHP.GT/WebEngine.