如何等待承诺在另一个 class 中完成
How to wait for promise to complete in another class
在另一个 class 中,我有一个 promise 可以正常工作。我需要在另一个控制器中使用返回的数据,但我不知道如何在另一个控制器中等待数据:
class PromiseController
{
private function load()
{
$client = new \GuzzleHttp\Client();
// variables omitted for example
$promise = $client->requestAsync('POST', $url, $options);
$json = null;
$promise->then(
function (ResponseInterface $res) {
$xml = simplexml_load_string($res->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
$json = json_encode($xml);
$json = $json;
// I see my json here. Great.
},
function (RequestException $e) {
Log::info($e->getMessage());
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
);
$return $json;
}
}
需要数据的控制器:
// Leaving out the function etc
$data = ( new PromiseController )->load();
return array(
'xmlAsJson' => $data
);
返回的数据总是null
。我需要等待 "needed" 控制器中的数据,但是如何呢?在将结果传递给 array
.
之前,我希望有一个单独的控制器来处理 xml 到 json
如果你想传播异步,你必须继续承诺,所以return来自你的控制器的新承诺:
class PromiseController
{
private function load()
{
$client = new \GuzzleHttp\Client();
$promise = $client->requestAsync('POST', $url, $options);
$jsonPromise = $promise->then(
function (ResponseInterface $res) {
$xml = simplexml_load_string($res->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
$json = json_encode($xml);
return $json;
},
function (RequestException $e) {
Log::info($e->getMessage());
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
);
return $jsonPromise;
}
}
稍后在代码中对生成的承诺调用 ->wait()
:
$data = ( new PromiseController )->load()->wait();
return array(
'xmlAsJson' => $data
);
在另一个 class 中,我有一个 promise 可以正常工作。我需要在另一个控制器中使用返回的数据,但我不知道如何在另一个控制器中等待数据:
class PromiseController
{
private function load()
{
$client = new \GuzzleHttp\Client();
// variables omitted for example
$promise = $client->requestAsync('POST', $url, $options);
$json = null;
$promise->then(
function (ResponseInterface $res) {
$xml = simplexml_load_string($res->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
$json = json_encode($xml);
$json = $json;
// I see my json here. Great.
},
function (RequestException $e) {
Log::info($e->getMessage());
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
);
$return $json;
}
}
需要数据的控制器:
// Leaving out the function etc
$data = ( new PromiseController )->load();
return array(
'xmlAsJson' => $data
);
返回的数据总是null
。我需要等待 "needed" 控制器中的数据,但是如何呢?在将结果传递给 array
.
如果你想传播异步,你必须继续承诺,所以return来自你的控制器的新承诺:
class PromiseController
{
private function load()
{
$client = new \GuzzleHttp\Client();
$promise = $client->requestAsync('POST', $url, $options);
$jsonPromise = $promise->then(
function (ResponseInterface $res) {
$xml = simplexml_load_string($res->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
$json = json_encode($xml);
return $json;
},
function (RequestException $e) {
Log::info($e->getMessage());
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
);
return $jsonPromise;
}
}
稍后在代码中对生成的承诺调用 ->wait()
:
$data = ( new PromiseController )->load()->wait();
return array(
'xmlAsJson' => $data
);