TIME
1:46 PM
Asynchronous Programming in Hack - Example Code
Asynchronous programming allows a script to run multiple tasks in parallel. These tasks are usually input/output operations that rely on operations that are run by separate servers or other OS processes.
Traditionally PHP executes those tasks and waits for them to finish before it returns the control to your script. This is called synchronous programming. Also, PHP has limited support for asynchronous programming, and it does not provide a clean and consistent solution.
With synchronous programming each task can only be started after the previous task has finished. Imagine a script that collects information from different sites. Each one will hold your script for as long as the servers take to respond. Thus, synchronous programming is often a waste of performance.
Hack made it easy to do asynchronous programming cleanly, and also without the pain of callbacks used by JavaScript solutions. It introduced the async keyword which you can assign to functions to tell that it can be executed asynchronously. There is also an await keyword, which is used to suspend the execution of the code that follows the statement, even if it is doing something asynchronous like waiting for some IOs or network connections etc.
This is a very elegant asynchronous programming solution because you do not have to be concerned with callbacks or internal event loops, to make your script hold in a certain position while parallel tasks keep running and do other things. This is definitely one of the best features of the Hack language.
class Foo {}
class Bar {
public function getFoo(): Foo {
return new Foo();
}
}
async function gen_foo(int $a): Awaitable<?Foo>; {
if ($a === 0) return null;
$bar = await gen_bar($a);
if ($bar !== null) {
return $bar->getFoo();
}
return null;
}
async function gen_bar(int $a): Awaitable<?Bar> {
if ($a === 0) return null;
return new Bar();
}
gen_foo(4);
Traditionally PHP executes those tasks and waits for them to finish before it returns the control to your script. This is called synchronous programming. Also, PHP has limited support for asynchronous programming, and it does not provide a clean and consistent solution.
With synchronous programming each task can only be started after the previous task has finished. Imagine a script that collects information from different sites. Each one will hold your script for as long as the servers take to respond. Thus, synchronous programming is often a waste of performance.
Hack made it easy to do asynchronous programming cleanly, and also without the pain of callbacks used by JavaScript solutions. It introduced the async keyword which you can assign to functions to tell that it can be executed asynchronously. There is also an await keyword, which is used to suspend the execution of the code that follows the statement, even if it is doing something asynchronous like waiting for some IOs or network connections etc.
This is a very elegant asynchronous programming solution because you do not have to be concerned with callbacks or internal event loops, to make your script hold in a certain position while parallel tasks keep running and do other things. This is definitely one of the best features of the Hack language.
class Foo {}
class Bar {
public function getFoo(): Foo {
return new Foo();
}
}
async function gen_foo(int $a): Awaitable<?Foo>; {
if ($a === 0) return null;
$bar = await gen_bar($a);
if ($bar !== null) {
return $bar->getFoo();
}
return null;
}
async function gen_bar(int $a): Awaitable<?Bar> {
if ($a === 0) return null;
return new Bar();
}
gen_foo(4);