Skip to content

Async event loop

Greg Bowler edited this page May 4, 2026 · 5 revisions

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.

A minimal loop

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() becomes false

Using PHP.GT/Async

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.

Inside WebEngine

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.

Clone this wiki locally