V8PromiseFactory

V8 has support for ES6 Promises and they make a clean JS-side API. So why not create promises from PHP, (later on) being resolved by PHP? V8Js doesn’t allow direct creation of JS objects from PHP-code, a little JS-side helper needs to be used. One possibility is this: class V8PromiseFactory { private $v8; public function __construct(V8Js $v8) { $this->v8 = $v8; } public function __invoke($executor) { $trampoline = $this->v8->executeString( '(function(executor) { return new Promise(executor); })'); return $trampoline($executor); } } … it can be used to construct an API method that returns a Promise like this:...

Planted: Mar 25, 2016 · 2 min