我正在尝试使用ReactPHP来理解Promise的概念
$app = function ($request, $response) use ($redis, $config) {
$promise = React\Promise\all(
array(
AsyncGetUser(),
AsyncGetDB(),
AsyncGetTemplate()
)
)->then(function ($res) {
$result = ParseTemplate($user, $template, $whatever);
}
\React\Promise\resolve($promise);
$response->writeHead(200, array('Content-Type' => 'text/plain'));
$response->end($result);
}
$http->on('request', $app);但是在$result准备好之前就发送了$response。怎样才能正确地发送$result等待$promise?
我尝试将函数移到another->then()部分,但在浏览器中没有得到任何响应(即,当$app = $response->end已经完成时,脚本会得到结果)。
发布于 2017-02-09 11:05:17
我根本不知道reactphp,但是如果promises像JS中的promises一样工作,看起来你需要在->then中编写响应,在那里你会得到一个结果!
$app = function ($request, $response) use ($redis, $config) {
$promise = React\Promise\all(
array(
AsyncGetUser(),
AsyncGetDB(),
AsyncGetTemplate()
)
)->then(function ($res) {
$result = ParseTemplate($user, $template, $whatever);
$response->writeHead(200, array('Content-Type' => 'text/plain'));
$response->end($result);
}
}
$http->on('request', $app);注意:代码中的以下行
\React\Promise\resolve($promise);这没有任何意义。\React\Promise\resolve并不像您想象的那样“解析承诺”,它创建并返回一个解析的承诺--您可以丢弃它!
https://stackoverflow.com/questions/42121428
复制相似问题