Skip to content

Instantly share code, notes, and snippets.

@cburgdorf
Last active March 3, 2020 00:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cburgdorf/9713936 to your computer and use it in GitHub Desktop.
Save cburgdorf/9713936 to your computer and use it in GitHub Desktop.
A simple example of how to use async/await with hack-lang to perform non-blocking async tasks.
<?hh
async function helloAfter($name, $timeout) {
// sleep asynchronously -- let other async functions do their job
await SleepWaitHandle::create($timeout * 1000000);
echo sprintf("hello %s\n", $name);
}
async function run() {
$joe = helloAfter('Joe', 3);
$mike = helloAfter('Mike', 1);
// wait for both dependencies
await GenArrayWaitHandle::create(array($joe, $mike));
}
run()->join();
// prints:
// hello Mike
// hello Joe
//The example is based on the discussion provided here: https://github.com/facebook/hhvm/issues/1494
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment