我正在使用AWS SDK,并有一个承诺:
public function foo(){
...
$pool = new CommandPool($client, $commands, [
....
]);
$promise = $pool->promise();
$result = $promise->wait();
$promise->then(function () {
return 'ok';
});
}
如何从foo
同步返回promise的结果?
我试过:
return $promise->then(function () {
return 'ok';
});
但这返回承诺本身,而不是'ok'
,并导致一个错误在我的框架:
The Response content must be a string or object implementing __toString(), "object" given.
AWS SDK使用guzzle/promises,它们可以使用wait
方法同步解析:
return $promise->then(function () { return 'ok'; })->wait();
参见https://github.com/guzzle/promises#synchronous-wait和http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/promises.html