Skip to content

Commit f63054e

Browse files
committed
Added basic next-tick/future-tick example.
1 parent 39ca8b1 commit f63054e

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

examples/next-tick.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
This example shows how nextTick and futureTick events are scheduled for
5+
execution.
6+
7+
The expected output is:
8+
9+
next-tick #1
10+
next-tick #2
11+
future-tick #1
12+
timer
13+
future-tick #2
14+
15+
Note that both nextTick and futureTick events are executed before timer and I/O
16+
events on each tick.
17+
18+
nextTick events registered inside an existing nextTick handler are guaranteed
19+
to be executed before timer and I/O handlers are processed, whereas futureTick
20+
handlers are always deferred.
21+
*/
22+
23+
require __DIR__.'/../vendor/autoload.php';
24+
25+
$loop = React\EventLoop\Factory::create();
26+
27+
$loop->addTimer(
28+
0,
29+
function () {
30+
echo 'timer' . PHP_EOL;
31+
}
32+
);
33+
34+
$loop->nextTick(
35+
function ($loop) {
36+
echo 'next-tick #1' . PHP_EOL;
37+
38+
$loop->nextTick(
39+
function () {
40+
echo 'next-tick #2' . PHP_EOL;
41+
}
42+
);
43+
}
44+
);
45+
46+
$loop->futureTick(
47+
function ($loop) {
48+
echo 'future-tick #1' . PHP_EOL;
49+
50+
$loop->futureTick(
51+
function () {
52+
echo 'future-tick #2' . PHP_EOL;
53+
}
54+
);
55+
}
56+
);
57+
58+
$loop->run();

0 commit comments

Comments
 (0)