-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from clue-labs/any
Add any() helper to await first successful fulfillment of operations
- Loading branch information
Showing
6 changed files
with
365 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
use Clue\React\Buzz\Browser; | ||
use Clue\React\Mq\Queue; | ||
use Psr\Http\Message\ResponseInterface; | ||
use React\EventLoop\Factory; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
// list of all URLs you want to try | ||
// this list may potentially contain hundreds or thousands of entries | ||
$urls = array( | ||
'http://www.github.com/invalid', | ||
'http://www.yahoo.com/invalid', | ||
'http://www.bing.com/invalid', | ||
'http://www.bing.com/', | ||
'http://www.google.com/', | ||
'http://www.google.com/invalid', | ||
); | ||
|
||
$loop = Factory::create(); | ||
$browser = new Browser($loop); | ||
|
||
// each job should use the browser to GET a certain URL | ||
// limit number of concurrent jobs here to avoid using excessive network resources | ||
$promise = Queue::any(2, $urls, function ($url) use ($browser) { | ||
return $browser->get($url)->then( | ||
function (ResponseInterface $response) use ($url) { | ||
// return only the URL for the first successful response | ||
return $url; | ||
} | ||
); | ||
}); | ||
|
||
$promise->then( | ||
function ($url) { | ||
echo 'First successful URL is ' . $url . PHP_EOL; | ||
}, | ||
function ($e) { | ||
echo 'An error occured: ' . $e->getMessage() . PHP_EOL; | ||
} | ||
); | ||
|
||
$loop->run(); |
This file contains 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
This file contains 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
Oops, something went wrong.