5

I have an array of promises.

I want to proceed only after all of the promises gave me a response no matter if they were resolved or reject. I thought that all() function can handle it, but it looks like it works only when all of the promises in the array are resolved and without considering rejections for some of the promises.

What function can I use??

example: the function getUser returns a promise object. When all of the promises gave me a response, i would like to catch the trigger, whether the promise is resolved or rejected.

array_push($this->users['users'], $this->userFetcher->getUser($userName));

Thanks :)

Ohad Koren
  • 121
  • 2
  • 5
  • I'm not sure why all the downvotes, this is a legit question for using reactphp promises. The JS version has over 80 upvotes and this one had -3. – Benjamin Gruenbaum Aug 22 '16 at 14:16

1 Answers1

3

Use all():

$getAllUsers = React\Promise\all($this->users['users']);
$getAllUsers->then(function ($users) { 
   echo "Got all users" . $users;
});
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 1
    Hey, Thank you very much, The problem with the all function is that it fails if any of the promises is rejected... I asked Jan this same question yesterday (the one who wrote the react PHP libary) and he told me that unfortunately react PHP doesn't have this ability... – Ohad Koren Aug 24 '16 at 08:25
  • Sure it does, add a catch handler to all promises - this JS question should give you the idea: http://stackoverflow.com/questions/31424561/wait-until-all-es6-promises-complete-even-rejected-promises – Benjamin Gruenbaum Aug 24 '16 at 08:27