Last active
December 19, 2015 04:49
-
-
Save kaja47/5900076 to your computer and use it in GitHub Desktop.
Generators and Promises in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // synchronous code | |
| // fetch operations are blocking | |
| $profile = fetchProfile('@kaja47'); | |
| $ids = []; | |
| $cursor = '-1'; | |
| do { | |
| $fs = fetchFollowers($profile->id, $cursor); | |
| $ids = array_merge($ids, $fs->ids); | |
| $cursor = $fs->next_cursor_str; | |
| } while ($cursor != "0") | |
| return [$profile, $ids]; | |
| // asynchronous code | |
| // fetch operations and flow function return promise | |
| // flow is implemented here: https://github.com/kaja47/flow/blob/master/flow.php | |
| flow(function() { | |
| $profile = (yield fetchProfile('@kaja47')); | |
| $ids = []; | |
| $cursor = '-1'; | |
| do { | |
| $fs = (yield fetchFollowers($profile->id, $cursor)); | |
| $ids = array_merge($ids, $fs->ids); | |
| $cursor = $fs->next_cursor_str; | |
| } while ($cursor != "0") | |
| yield [$profile, $ids]; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment