Hacker News new | past | comments | ask | show | jobs | submit login

With Async functions I'm not sure if the documentation is lacking or I'm just slightly thick, I believe the later.

(I'm not a php developer so excuse my ignorance)

It seems you only have 1 method 'await', to check if the async function has completed its job (it self, if I'm understanding is a blocking operation in which ever thread/worker its called).

So is there a way to run something more similar to

      $i = 1;
      while(doingSomeThing)
      {
             $b = "";
             if(completed gen_foo())
             {
                    $b += await gen_foo();
                    do_critical_task($b);
             }
             else
             {
                    do_something(do_item[i]);
             }
       $i++; 
       }
Because as I see it now, what ever I run asynchronously I.E.:

       gen_foo(get_user_data); //async
       $x = do_something_for_a_while(); //do something while gen foo processes
       do_new_thing($x, await gen_foo()); //do something with rendezvous result
I'm forced to time out my async calls so that they'll rendezvous in the same place won't I?

Again if I'm completely off the mark please let me know.




async/await allows cooperative multitasking on a single thread: stay tuned for more...

The way it works is quite different from your example:

  async function gen_foo(): Awaitable<string> {
    echo "until we get to an await, eager execution\n";
    // ...
    await gen_bar();
    // this code is only executed after await
    return 'result';
  }
  $x = gen_foo(); // x is a handle, suspended at the point of its first 'await'
  await $x; // ... and now it's resumed
  
The benefit comes from being able to batch these async functions together:

  list($x, $y, $z) = await genva(
    gen_foo(), 
    gen_bar(),
    gen_baz();
  ); // genva creates a wait handle for awaiting its args

  // ... and when we get here $x, $y, and $z are all assigned




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: