2

我正在循环访问用户数据库以将数据传递给 API 以通过getAsync. 我需要根据 API 返回的内容更新我的数据库记录。问题是我不知道如何传递$user->id给更新记录的承诺。

$users = User::all();
$client = new Client();

$promises = (function () use ($users, $client) {
   foreach ($users as $user) {
        yield $client->getAsync('https://some/api/' . $user->address);
    }
})();

$eachPromise = new EachPromise($promises, [
    'concurrency' => 2,
    'fulfilled' => function (Response $response) {
        if ($response->getStatusCode() == 200) {
            //NEED TO ACCESS USER->ID HERE TO UPDATE RECORD IN DB                   
        }
    },
    'rejected' => function ($reason) {
    
    }
]);
$eachPromise->promise()->wait();

提前致谢!

4

0 回答 0