File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -23,23 +23,21 @@ function download(array $urls)
2323 $ loop = Factory::create ();
2424 $ browser = new Browser ($ loop );
2525
26- $ queue = new Queue (3 , null , function ($ url ) use ($ browser ) {
27- return $ browser ->get ($ url );
28- });
29-
30- $ promises = array ();
31- foreach ($ urls as $ url ) {
32- $ promises [$ url ] = $ queue ($ url )->then (
33- function (ResponseInterface $ response ) use ($ url ) {
26+ $ urls = array_combine ($ urls , $ urls );
27+ $ promise = Queue::all (3 , $ urls , function ($ url ) use ($ browser ) {
28+ return $ browser ->get ($ url )->then (
29+ function (ResponseInterface $ response ) {
30+ // return only the body for successful responses
3431 return $ response ->getBody ();
3532 },
36- function (Exception $ e ) use ($ url ) {
33+ function (Exception $ e ) {
34+ // return null body for failed requests
3735 return null ;
3836 }
3937 );
40- }
38+ });
4139
42- return Block \awaitAll ( $ promises , $ loop );
40+ return Block \await ( $ promise , $ loop );
4341}
4442
4543$ responses = download ($ urls );
You can’t perform that action at this time.
0 commit comments