Skip to content

Struggling with the simplest example #56

Closed

Description

@RobertEcker

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!

Activity

  1. added
    questionFurther information is requested
    on Jul 5, 2022
  2. added this to the v4.0.0 milestone on Jul 5, 2022
  3. clue commented on Jul 5, 2022

    @clue
    Member

    @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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions