@@ -58,6 +58,8 @@ private function processNextCharacter(string $charMap):void {
5858 }
5959
6060 if ($ this ->file ->eof ()) {
61+ // When the file reaches the end, this deferred task is finished. We can now
62+ // resolve the Deferred with the final value.
6163 $ this ->deferred ->resolve ($ this ->characterCount );
6264 return ;
6365 }
@@ -88,6 +90,9 @@ private function processNextCharacter(string $charMap):void {
8890$ loop = new Loop ();
8991$ loop ->addTimer ($ timer );
9092$ loop ->haltWhenAllDeferredComplete (true );
93+ $ loop ->addHaltCallback (function () {
94+ echo PHP_EOL , "Loop has halted because all Deferred tasks are complete. " ;
95+ });
9196
9297// Create the example classes to slowly loop over the characters of the file.
9398$ reader1 = new SlowFileReader ("example.txt " );
@@ -101,17 +106,17 @@ private function processNextCharacter(string $charMap):void {
101106// by the Loop's timers.
102107$ reader1 ->countCharacters ("aeiou " )
103108->then (function (int $ numVowels ):void {
104- echo "Example text has $ numVowels vowels. " , PHP_EOL ;
109+ echo PHP_EOL , "Example text has $ numVowels vowels. " ;
105110});
106111
107112// Another Promise can be added, so their Deferred's work is undertaken
108113// concurrently.
109114$ reader2 ->countCharacters ("bcdfghjklmnpqrstvwxyz " )
110115->then (function (int $ numConsonants ):void {
111- echo "Example text has $ numConsonants consonants. " , PHP_EOL ;
116+ echo PHP_EOL , "Example text has $ numConsonants consonants. " ;
112117});
113118
114119// Here we execute the loop, which has been set to halt when all Deferred
115120// objects complete.
116121$ loop ->run ();
117- echo "Complete! " , PHP_EOL ;
122+ echo PHP_EOL , "Complete! " , PHP_EOL ;
0 commit comments