请注意,我还在相关的存储库上创建了一个issue。
在documentation中,它表示此函数将返回一个promise,该promise将在数组中的所有promise解析后解析。
下面是我的实现;
private function downloadListingImages($contents)
{
    $response = [];
    $name = 1;
    foreach ($contents['images'] as $image) {
        $response[] = $this->downloadImage($image, $name);
        $name++;
    }
    return $response;
}
private function downloadImage($link, $name)
{
    $guzzle = new Client([
        'handler' => HandlerStack::create(new HttpClientAdapter($this->loop)),
    ]);
    $promise = $guzzle->getAsync($link, [
        'save_to' => 'assets/' . $name . '.jpg',
    ]);
    return $promise;
}
$promises = $this->downloadListingImages($contents);现在,到目前为止一切都很好。但我想要做的是从对我的服务器的请求中获取$contents。所以我有一个服务器实现;
$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) use ($promises) {
    \React\Promise\all($promises)->always(function($val) {
        file_put_contents('meh.txt', "meh");
    });
    return new React\Http\Response(
        200,
        array('Content-Type' => 'text/plain'),
        "Hello World!\n"
    );
});我在这里期望$server返回一个即时响应(它确实返回了),一段时间后在我的代码库中看到meh.txt。但是,它永远不会落在always回调上。即使我没有在all方法上链接任何函数,它也会自动解析。难道不应该等到调用then或类似的东西来解决吗?怎样才能异步运行我的get,并在工作完成时得到通知?
发布于 2018-09-27 21:55:08
只要传递到\React\Promise\的对象都实现了该方法,它就能很好地工作。例如,\React\Promise\all与\React\Promise\Promise()和\GuzzleHttp\Promise\Promise一起工作得非常好。
到目前为止,我不能重现这个问题。
正如您在文档中看到的,always-method不接受任何参数。如果需要参数,请考虑使用then而不是always。还要考虑到file_put_contents可能会阻塞事件循环。
我希望这能帮到你。
https://stackoverflow.com/questions/52480159
复制相似问题