我保证会发送正常的电子邮件。
$promise = $this->SesClient->sendEmailAsync($messages[0]);
$promise->then(
function ($value) {
echo "The promise was fulfilled with {$value}";
},
function ($reason) {
echo "The promise was rejected with {$reason}";
}
);
但是,我希望能够合并所有电子邮件,并同时发送所有电子邮件,同时仍然要进行回调。根据电子邮件发送成功还是失败,我将其记录在数据库中。
我有类似的东西,可以很好地发送我的所有电子邮件,但是我该如何使用->然后对每个诺言在完成后对每个诺言执行一个操作?
$promises = [];
foreach($messages as $message) {
$promises[] = $this->SesClient->sendEmailAsync($message['Email']);
}
$results = Promise\unwrap($promises);
GuzzleHttp\Promise
为最终用户提供了流畅的界面。调用GuzzleHttp\Promise\PromiseInterface::then
的结果是GuzzleHttp\Promise\PromiseInterface
的实例。这使最终用户能够链接多个then()
块。
要在每个响应的基础上完成多个任务,所有要做的就是将随后的块“链接”
foreach($messages as $message) {
$promises[] = $this->SesClient->sendEmailAsync($message['Email'])
->then(function (SomeDataType $inputData) {
echo 'process some value in here';
return $processedValue
})
->then(function (ProcessedDataType $value) {
echo 'store the processed value in the database';
});
}
甚至可能将值从一个承诺传递到下一个承诺。
可以在here中找到有关枪口承诺的更多信息>