-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleAsync.php
More file actions
61 lines (47 loc) · 1.28 KB
/
ExampleAsync.php
File metadata and controls
61 lines (47 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?hh // strict
// Example name: async
class ExampleAsync implements ExampleInterface
{
public function __construct() {}
public function run(): void
{
$first = $this->firstAsync();
$second = $this->secondAsync();
// Vector<Awaitable<int>>
$vectorOfAwaitables = Vector {$first, $second};
// Awaitable<Vector<int>>
$singleAwaitable = HH\Asio\v($vectorOfAwaitables);
// Vector<int>
$resolvedVector = HH\Asio\join($singleAwaitable);
$finalResult = $resolvedVector->at(0) + $resolvedVector->at(1);
echo "Final result is $finalResult\n";
}
private async function firstAsync(): Awaitable<int>
{
return await $this->computeResult(__FUNCTION__, 7, 3);
}
private async function secondAsync(): Awaitable<int>
{
return await $this->computeResult(__FUNCTION__, 5, 2);
}
private async function computeResult(
string $funcName,
int $sleepTime,
int $finalResult
): Awaitable<int>
{
echo "$funcName invoked and sleeping for $sleepTime seconds\n";
$url = sprintf(
'http://127.0.0.1:8080/server.php?sleep=%d&return=%d',
$sleepTime,
$finalResult
);
// Awaitable<string>
$curlResult = HH\Asio\curl_exec($url);
// string
$noAwaitable = await $curlResult;
$result = intval($noAwaitable);
echo "$funcName is returning $result\n";
return $result;
}
}