Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struggling with the simplest example #56

Closed
RobertEcker opened this issue Jul 4, 2022 · 1 comment
Closed

Struggling with the simplest example #56

RobertEcker opened this issue Jul 4, 2022 · 1 comment
Labels
question Further information is requested
Milestone

Comments

@RobertEcker
Copy link

RobertEcker commented Jul 4, 2022

Hi everybody,

<?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.

  1. There is an unexpected token "echo" in line 16.
  2. When I comment line 16 with // in the beginning, the output is just 'a' (without 'c') - why not 'ac'?

Thank you!

@clue clue added the question Further information is requested label Jul 5, 2022
@clue clue added this to the v4.0.0 milestone Jul 5, 2022
@clue
Copy link
Member

clue commented 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:

<?php

require __DIR__ . '/vendor/autoload.php';

use React\EventLoop\Loop;

Loop::addTimer(0.5, React\Async\async(function () {
    echo 'a';
    React\Async\await(React\Promise\Timer\sleep(1.0));
    echo 'c';
}));

Loop::addTimer(1.0, function () {
    echo 'b';
});
  1. There is an unexpected token "echo" in line 16.

Most notably, arrow function do in fact not support echo because it is a language construct and not a function:

- Loop::addTimer(1.0, fn() => echo 'b');
+ Loop::addTimer(1.0, function () {
+     echo 'b';
+ });

Less notably, the previous example used the wrong namespace casing (minor oversight that should not cause any problems):

-    React\async\await(React\Promise\Timer\sleep(1.0));
+    React\Async\await(React\Promise\Timer\sleep(1.0));

I've just filed #57 to fix the examples.

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 👍

@clue clue closed this as completed Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants