{"action":"opened","issue":{"url":"https://api.github.com/repos/phpv8/v8js/issues/208","repository_url":"https://api.github.com/repos/phpv8/v8js","labels_url":"https://api.github.com/repos/phpv8/v8js/issues/208/labels{/name}","comments_url":"https://api.github.com/repos/phpv8/v8js/issues/208/comments","events_url":"https://api.github.com/repos/phpv8/v8js/issues/208/events","html_url":"https://github.com/phpv8/v8js/issues/208","id":137510760,"number":208,"title":"Promise support","user":{"login":"stesie","id":113068,"avatar_url":"https://avatars.githubusercontent.com/u/113068?v=3","gravatar_id":"","url":"https://api.github.com/users/stesie","html_url":"https://github.com/stesie","followers_url":"https://api.github.com/users/stesie/followers","following_url":"https://api.github.com/users/stesie/following{/other_user}","gists_url":"https://api.github.com/users/stesie/gists{/gist_id}","starred_url":"https://api.github.com/users/stesie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stesie/subscriptions","organizations_url":"https://api.github.com/users/stesie/orgs","repos_url":"https://api.github.com/users/stesie/repos","events_url":"https://api.github.com/users/stesie/events{/privacy}","received_events_url":"https://api.github.com/users/stesie/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/phpv8/v8js/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-03-01T08:47:13Z","updated_at":"2016-03-01T08:47:13Z","closed_at":null,"body":"Promises are an increasingly popular topic in JS world, and V8 supports ES6 promises well since quite a few releases\r\n\r\nPassing Promise instances from JS to PHP should already work by normal wrapping means, i.e. V8Object.  This is PHP can register methods via `then` and `catch` as needed, which are wrapped to V8 native functions like usual.\r\n\r\nCreating promises is more involved, either you ship a promise implementation on PHP side ... then normal existing wrapping means can be used.  Or you need a JS-side \"factory\" that does the instanciation.\r\n\r\n```php\r\n$v8 = new V8Js();\r\n$promiseFactory = $v8->executeString(<<<EOF\r\n(function _promiseFactory($resolver) {\r\n  return new Promise($resolver);\r\n});\r\n```\r\n\r\nthat would allow you to\r\n\r\n```php\r\n$promise = $promiseFactory(function($resolve, $reject) {\r\n  // either sync or async implementation of the promise here\r\n  $resolve('the_result');\r\n});\r\nreturn $promise;  // whatever ...\r\n```\r\n\r\n... the whole `$promiseFactory` is implicitly bound to `$v8` as it actually is just a plain `V8Object`.\r\n\r\nV8Js could provide some `V8Promise` class, which might try to mimic JS' `new Promise` syntax like\r\n\r\n```php\r\n$promise = new V8Promise(function($resolve, $reject) {\r\n  ....\r\n});\r\nreturn $promise; // ... pass $promise back to JS\r\n```\r\n\r\n... this initially does *not* bind the `V8Promise` to a V8 instance, which is kindof bad.\r\nThe straight forward option would be to add a `\\V8Js` reference as first argument to the `V8Promise::__construct` constructor method.  This would allow even PHP code to immediately call `then` and `catch` to register own handlers.\r\n\r\nAnother, more magic, option would be to implicitly bind the `V8Promise` to a V8 instance once it is passed to V8.  However that way `then` and `catch` won't be usable until passed back to V8 -- and in the end we might need some explicit `bind`.\r\n\r\n\r\nWhat do you think?  Should V8Js provide a `V8Promise` class? Which signature?"}}