Last active
March 3, 2020 00:42
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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