Skip to content

Instantly share code, notes, and snippets.

@kaja47
Last active December 19, 2015 04:49
Show Gist options
  • Select an option

  • Save kaja47/5900076 to your computer and use it in GitHub Desktop.

Select an option

Save kaja47/5900076 to your computer and use it in GitHub Desktop.
Generators and Promises in PHP
<?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