In a new post to his site Sergey Zhuk has a tutorial showing you how to manage promises in ReactPHP. Since promises are fired asynchronously they can be difficult to manage and use their output across the application.
Asynchronous application is always a composition of independently executing things. In concurrency, we are dealing with a lot of different things at once. [...] So, to make concurrency work you have to create a communication between these independent parts to coordinate them. And here come promises. They are the basic unit of concurrency in an asynchronous application. They are the blood of the asynchronous application and move the results between different tasks across the code.
He then covers a few different situations and offers advice on how to more correctly handle them:
- I don’t know exactly what the resolver will give me
- I want to reject a promise but without throwing an exception
- I want to run multiple tasks and when they all finish do something else
- I have some pending tasks and want to continue once I receive the first feedback
- I have some pending tasks and want to continue once the first one is completed
- I have some pending tasks and want to continue once a certain number of tasks will be completed
Code is provided for each of the situations giving you an easy, ready to use example for your application. Most require only a few lines to get the job done and can be very useful in the right circumstances.