同步返回AWS SDK承诺的结果?

时间:2016-10-12 12:10:20

标签: php aws-sdk

我正在使用AWS SDK并承诺:

public function foo(){
    ...
    $pool = new CommandPool($client, $commands, [
        ....
    ]);
    $promise = $pool->promise();
    $result = $promise->wait();
    $promise->then(function () {
          return 'ok';
    });
}

如何从foo同步返回承诺的结果?

我试过了:

return $promise->then(function () {
       return 'ok';
 });

但是这会返回promise本身,而不是'ok',并导致我的框架出错:

The Response content must be a string or object implementing __toString(), "object" given.

1 个答案:

答案 0 :(得分:3)

AWS SDK使用guzzle / promises,可以使用wait方法同步解析它们:

return $promise->then(function () { return 'ok'; })->wait();

请参阅https://github.com/guzzle/promises#synchronous-waithttp://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/promises.html