Skip to content

Instantly share code, notes, and snippets.

@sroze
Last active April 3, 2018 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sroze/2ed22483b5fd35081b4fa31f02fd1a24 to your computer and use it in GitHub Desktop.
Save sroze/2ed22483b5fd35081b4fa31f02fd1a24 to your computer and use it in GitHub Desktop.
<?php
// Expected output:
// Got exception do nothing
// Got message test2
// Got message test3
// Actual output:
// Got exception do nothing
// Got message test3
class Message {
public $str;
public function __construct($str) {
$this->str = $str;
}
}
function receive(): iterable
{
$messages = [new Message('test1'), new Message('test2'), new Message('test3')];
while (true) {
if (null === $message = array_shift($messages)) {
break;
}
try {
yield $message;
} catch (\Throwable $e) {
echo 'Got exception do nothing'.PHP_EOL;
}
}
}
$throwed = false;
$handler = function ($t) use (&$throwed) {
if (false === $throwed) {
$throwed = true;
throw new \Exception('Fail');
}
echo sprintf('Got message %s', $t->str).PHP_EOL;
};
$iterator = receive();
foreach ($iterator as $message) {
try {
$handler($message);
} catch (\Throwable $e) {
$iterator->throw($e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment