I've been reading about the Promise pattern, and I tried to code up a version in PHP with the help of a few examples and my own understanding of how it should work. Is what I came up with a reasonable example of the Promise pattern, or did I implement this incorrectly?
class PromiseClass {
private $callbacks = array();
private $last_return;
function promise($promise) {
if (get_class($promise) == 'Promise') {
return $promise;
} else if (is_callable($promise)) {
$this->then($promise);
return $this;
}
}
public function then (callable $callback) {
$this->callbacks[] = $callback;
return $this;
}
public function resolve () {
$callback = array_shift($this->callbacks);
if (is_callable($callback)) {
$this->last_return = $callback($this->last_return);
}
if (count($this->callbacks) > 0) {
$this->resolve();
}
}
}
Example use:
$promiser->promise(function() {
echo "sleeping\n";
sleep(3);
return 3;
})
->then(function($args) {
echo " in second function, $args\n";
});
$promiser->resolve();
A promise represents a single result of an asynchronous operation. It is not necessarily available at a specific time, but should become in the future. The PHP-HTTP promise follows the Promises/A+ standard.
Closures and Promise are different concepts. Closures refers to scope of variables where as promise are used to 'promise' that an act on something will occur when it is done on an asynchronous action.
A Promise is an object that represents the eventual completion (or failure) of an asynchronous operation, and its resulting value. var promise = new Promise(function(resolve, reject) { // do thing, then… if (/* everything worked */) { resolve("See, it worked!"); } else { reject(Error("It broke")); } }); ADVERTISEMENT.
A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object. It may be either fulfilled or rejected — for example, resolving a rejected promise will still result in a rejected promise.
Your Promise implementation is mostly correct. There is, however, a problem: that, at least in PHP, it is mostly useless and almost completely equivalent to the Observer pattern wikipedia msdn oodesign.
PHP is almost completely single-threaded. sleep
is no exception. As such, your entire Promise will block your script until it is complete. As a result, seeing as the operations are executed inline, you might as well not bother.
A possible way to get rid of that little problem would be to cause your Promise to fork from the main script, which is possible using the PCNTL family of functions. This will allow the Promise code to run in the background, while the main script continues. When the Promise completes, it comes back.
A way to do this is outlined at http://www.php.net/manual/en/function.pcntl-fork.php#98711 . It makes active use of pcntl_fork
, which allows you to fork a new thread. It has drawbacks - the biggest of them being the inability to message the main process by anything but signals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With