Your Promise implementation is basically correct. However, there is a problem: at least in PHP it is mostly useless and almost completely equivalent to the Observer wikipedia msdn oodesign pattern .
PHP is almost completely single-threaded. sleep no exception. This way all your Promise will block your script until it is complete. As a result, when operations are performed on a line, you also need not worry.
A possible way to get rid of this small problem is to force Promise to evolve from the main script, which is possible using the PCNTL family of functions. This will allow you to use Promise code in the background while the main script continues. When the promise is completed, it is returned.
The way to do this is described in http://www.php.net/manual/en/function.pcntl-fork.php#98711 . It makes extensive use of pcntl_fork , which allows you to fork a new thread. This has flaws - the biggest of them - the inability to convey the main process with anything but signals.
Sebastien renauld
source share