如何让Guzzle Promises异步运行?

时间:2018-01-09 05:19:16

标签: php promise guzzle

摄制:

<?php
use GuzzleHttp\Promise\Promise;
require __DIR__ . '/vendor/autoload.php';

$promise1 = new Promise(function() use (&$promise1) {
    print 'PROMISE 1 FIRED' . PHP_EOL;
    sleep(2);

    $promise1->resolve('foo');
});

$promise2 = new Promise(function() use (&$promise2) {
    print 'PROMISE 2 FIRED' . PHP_EOL;
    sleep(2);

    $promise2->resolve('bar');
});

$promises = [$promise1, $promise2];
$results = \GuzzleHttp\Promise\unwrap($promises);

var_dump($results);

转储是:

//PROMISE 1 FIRED
//PROMISE 2 FIRED <-- fires after 2 seconds, and not immediately
//array(2) {
//    [0]=>
//  string(3) "foo"
//    [1]=>
//  string(3) "bar"
//}

现在,PROMISE 2 FIRED会在2秒后打印,而不是立即打印。

如何立即打印?

0 个答案:

没有答案