current_coroutine

(PHP 8.6+, True Async 1.0)

current_coroutine() — Returns the object of the currently executing coroutine.

Description

current_coroutine(): Async\Coroutine

Return Values

An Async\Coroutine object representing the current coroutine.

Errors/Exceptions

Async\AsyncException — if called outside a coroutine.

Examples

Example #1 Getting the coroutine ID

<?php
use function Async\spawn;
use function Async\current_coroutine;

spawn(function() {
    $coro = current_coroutine();
    echo "Coroutine #" . $coro->getId() . "\n";
});
?>

Example #2 Diagnostics

<?php
use function Async\spawn;
use function Async\current_coroutine;

spawn(function() {
    $coro = current_coroutine();

    echo "Spawned from: " . $coro->getSpawnLocation() . "\n";
    echo "Status: " . ($coro->isRunning() ? 'running' : 'suspended') . "\n";
});
?>

See Also