2

在将 Guzzle 用于异步请求时,我希望在每个请求完成后立即获得相同的回调。

以下代码是有效的,但我认为它可能没有意义。

Guzzle 是否提供任何方法,Promise\settle($promises)->then($callback)->wait()但每个请求都完成了?

<?php
require("vendor/autoload.php");

use GuzzleHttp\Client;
use GuzzleHttp\Promise;

$client = new Client(['base_uri' => 'http://httpbin.org/']);

// I want got callback immediately when each request done.
$callback = function($response) {
    echo $response->getBody();
};

$promises = [
    $client->getAsync('/delay/8')->then($callback),
    $client->getAsync('/delay/4')->then($callback),
    $client->getAsync('/delay/2')->then($callback),
    $client->getAsync('/delay/1')->then($callback),
];
Promise\settle($promises)->wait();
4

0 回答 0