user2108258
user2108258

Reputation: 843

HHVM Class Undefined SleepWaitHandle

I'm trying to use HHVM's async functions in a larval application. I added the async keyword to my function but I get an error on the line with await SleepWaitHandle. It says that the class is undefined. It doesn't seem like the documentation has changed on this. What am I missing?

await \SleepWaitHandle::create(\DB::table('submissions')->insert($submissions_for_insert));

I had this happen on a 3.9 nightly and 3.8 stable. Running ubuntu 14.10.

I tried running a demo from http://hhvm.com/blog/7091/async-cooperative-multitasking-for-hack

<?hh

async function hello(): Awaitable<string> {
  return "Hello World";
}
async function goodbye(): Awaitable<string> {
  return "Goodbye, everybody!";
}
async function run(
  array<Awaitable<string>> $handles,
): Awaitable<array<string>> {
  await AwaitAllWaitHandle::fromArray($handles);
  return array_map($handle ==> $handle->result(), $handles);
}
$results = run(array(hello(), goodbye()))->getWaitHandle()->join();
print_r($results);
// Array
// (
//  [0] => Hello World
//  [1] => Goodbye, everybody!
// )

But running this on the command line returns

Catchable fatal error: Hack type error: Invalid argument at /test/asyn.php line 12

Upvotes: 0

Views: 486

Answers (1)

Josh Watzman
Josh Watzman

Reputation: 7260

  • To your first problem: \SleepWaitHandle in fact does not exist. The fully-qualified class name is \HH\SleepWaitHandle (or maybe \HH\Asio\SleepWaitHandle, I don't quite remember). If you notice, all the examples omit the leading \ -- in Hack code, several classes, such as SleepWaitHandle, are automatically imported into your current namespace, provided there is no conflicting class name. You need to either use this behavior, or use the correct fully-qualified name.
  • To your second problem, this is a bug in the example on the blog -- oops! The parameter to run should be array<WaitHandle<string>> $handles. I've updated the example in the blog post. It's a weird example though -- you normally don't work with AwaitAllWaitHandle directly; instead, you should be using the \HH\Asio\v() and \HH\Asio\m() functions, perhaps with support from the official asio-utilities composer package.

Upvotes: 1

Related Questions