success = false; $loop = new Loop(3); try { $loop->execute(function () use ($loop): void { if ($this->__fiveSecondsTillSuccess()) { $loop->end(); } }); } catch (Throwable $t) { $loop->end(); echo PHP_EOL; \var_dump($this->success); throw $t; } self::assertSame(true, $this->success); } private function __fiveSecondsTillSuccess(): bool { \sleep(5); $this->success = true; return true; } }