Skip to Main Content
Hack 与HHVM 权威指南
book

Hack 与HHVM 权威指南

by Owen Yamauchi
March 2017
Beginner content levelBeginner
247 pages
3h 55m
Chinese
China Machine Press
Content preview from Hack 与HHVM 权威指南
异步
129
以使用
await as
async function
countdown(int $start): AsyncKeyedIterator<int, string> {
for
($i = $start; $i >= 0; --$i) {
await
HH\Asio\usleep(1000000);
yield
$i => (string)$i;
}
}
async function
use_countdown(): Awaitable<void> {
foreach
(countdown(10)
await as
$num => $str) {
// $num
是个整型
// $str
是个字符串型
var_dump($num, $str);
}
}
最后,如果你希望在一个异步生成器上调用
send()
或者
raise()
方法,你需要使用接
AsyncGenerator
作为替代。它拥有三个类型实参:键的类型、值的类型,以及你希
望传递
send()
的类型
async function
namifier(): AsyncGenerator<int, string, int> {
//
得到第一个
id
$id =
yield
0 => '';
// $id
的类型为
int
while
($id !==
null
) {
$name =
await
get_name($id);
$id =
yield
$id => $name;
}
}
async function ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reinventing the Organization for GenAI and LLMs

Reinventing the Organization for GenAI and LLMs

Ethan Mollick

Publisher Resources

ISBN: 9787111554844