2017-02-08 20 views
1

을 약속 나는 $result가 준비되기 전에 전송 ReactPHP 및

$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); 

그러나 $response ReactPHP

를 사용하여 약속의 개념을 이해하기 위해 노력하고있어. 어떻게하면 $promise을 기다릴 수 있습니까? $result을 올바르게 보냅니 까?

$response->endanother->then() 섹션으로 이동하려고 시도했지만 브라우저에서 아무런 응답이 없습니다. 즉, $ app = function이 이미 완료되면 스크립트가 결과를 얻습니다.

답변

1

reactphp에 대해 전혀 알지 못합니다. 예를 들어 약속이 JS와 같이 작동하는 경우 결과가있는 ->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 당신이 생각하는 것처럼 "약속을 해결하지"않을 경우, 약속 한 약속이 생성되어 반환됩니다.