You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?php
require __DIR__ . '/async/vendor/autoload.php';
use React\EventLoop\Loop;
use React\Promise\Promise;
use function React\Async\await;
use function React\Async\async;
Loop::addTimer(0.5, React\Async\async(function() {
echo 'a';
React\async\await(React\Promise\Timer\sleep(1.0));
echo 'c'; // not echoed?
}));
Loop::addTimer(1.0, fn() => echo 'b'); // line 16
I am already struggling with the simplest example (copied from the main page).
I am using PHP 8.1.2 on Win10 with the newest async.
There is an unexpected token "echo" in line 16.
When I comment line 16 with // in the beginning, the output is just 'a' (without 'c') - why not 'ac'?
2. When I comment line 16 with // in the beginning, the output is just 'a' (without 'c') - why not 'ac'?
This is indeed a nasty one! The example uses the async sleep() function that first needs to be installed like this:
composer require react/promise-timer
If this package is not installed, PHP will actually throw an Error on this line:
Error: Call to undefined function React\Promise\Timer\sleep() in …/issue56.php:9
When you invoke the function returned from await(), it returns a promise. If you throw inside your function, the code will cause the promise to be rejected. Accordingly, the following lines in your function will no longer be executed.
Until reactphp/promise#87 is addressed (which is a major BC break due for the next major promise version), it will discard this error message unless you add some error reporting manually. This combined means it looks like it's silently failing, which is definitely not desired.
I agree that this is an easy oversight and definitely something we should improve in the future! I'll take a look as this as part of reactphp/promise#87 to improve default error reporting in this case and improve the API somewhat.
I hope this helps! I'll assume this is resolved and will close this for now, please feel free to report back otherwise 👍
Activity
clue commentedon Jul 5, 2022
@RobertEcker Thank you for reporting – and for helping us identify some smaller issues in the examples!
Here's an updated example from the updated documentation that should work just fine:
Most notably, arrow function do in fact not support
echobecause it is a language construct and not a function:Less notably, the previous example used the wrong namespace casing (minor oversight that should not cause any problems):
I've just filed #57 to fix the examples.
This is indeed a nasty one! The example uses the async
sleep()function that first needs to be installed like this:If this package is not installed, PHP will actually throw an
Erroron this line:When you invoke the function returned from
await(), it returns a promise. If you throw inside your function, the code will cause the promise to be rejected. Accordingly, the following lines in your function will no longer be executed.Until reactphp/promise#87 is addressed (which is a major BC break due for the next major promise version), it will discard this error message unless you add some error reporting manually. This combined means it looks like it's silently failing, which is definitely not desired.
I agree that this is an easy oversight and definitely something we should improve in the future! I'll take a look as this as part of reactphp/promise#87 to improve default error reporting in this case and improve the API somewhat.
I hope this helps! I'll assume this is resolved and will close this for now, please feel free to report back otherwise 👍