PageRenderTime 1451ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/next-tick.php

https://github.com/NandoKstroNet/react
PHP | 58 lines | 30 code | 9 blank | 19 comment | 0 complexity | 14f17a543bf3f25281f563fdf69ba70c MD5 | raw file
  1. <?php
  2. /*
  3. This example shows how nextTick and futureTick events are scheduled for
  4. execution.
  5. The expected output is:
  6. next-tick #1
  7. next-tick #2
  8. future-tick #1
  9. timer
  10. future-tick #2
  11. Note that both nextTick and futureTick events are executed before timer and I/O
  12. events on each tick.
  13. nextTick events registered inside an existing nextTick handler are guaranteed
  14. to be executed before timer and I/O handlers are processed, whereas futureTick
  15. handlers are always deferred.
  16. */
  17. require __DIR__.'/../vendor/autoload.php';
  18. $loop = React\EventLoop\Factory::create();
  19. $loop->addTimer(
  20. 0,
  21. function () {
  22. echo 'timer' . PHP_EOL;
  23. }
  24. );
  25. $loop->nextTick(
  26. function ($loop) {
  27. echo 'next-tick #1' . PHP_EOL;
  28. $loop->nextTick(
  29. function () {
  30. echo 'next-tick #2' . PHP_EOL;
  31. }
  32. );
  33. }
  34. );
  35. $loop->futureTick(
  36. function ($loop) {
  37. echo 'future-tick #1' . PHP_EOL;
  38. $loop->futureTick(
  39. function () {
  40. echo 'future-tick #2' . PHP_EOL;
  41. }
  42. );
  43. }
  44. );
  45. $loop->run();