AWS SDK承诺中的每个承诺回调

问题描述 投票:0回答:1

我保证会发送正常的电子邮件。

$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);
php amazon-web-services promise aws-sdk guzzle
1个回答
0
投票

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中找到有关枪口承诺的更多信息>

© www.soinside.com 2019 - 2024. All rights reserved.