{"action":"created","issue":{"url":"https://api.github.com/repos/m6w6/ext-pq/issues/12","repository_url":"https://api.github.com/repos/m6w6/ext-pq","labels_url":"https://api.github.com/repos/m6w6/ext-pq/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/m6w6/ext-pq/issues/12/comments","events_url":"https://api.github.com/repos/m6w6/ext-pq/issues/12/events","html_url":"https://github.com/m6w6/ext-pq/issues/12","id":153129757,"number":12,"title":"New async API for PHP 7","user":{"login":"ParkFramework","id":6284130,"avatar_url":"https://avatars.githubusercontent.com/u/6284130?v=3","gravatar_id":"","url":"https://api.github.com/users/ParkFramework","html_url":"https://github.com/ParkFramework","followers_url":"https://api.github.com/users/ParkFramework/followers","following_url":"https://api.github.com/users/ParkFramework/following{/other_user}","gists_url":"https://api.github.com/users/ParkFramework/gists{/gist_id}","starred_url":"https://api.github.com/users/ParkFramework/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ParkFramework/subscriptions","organizations_url":"https://api.github.com/users/ParkFramework/orgs","repos_url":"https://api.github.com/users/ParkFramework/repos","events_url":"https://api.github.com/users/ParkFramework/events{/privacy}","received_events_url":"https://api.github.com/users/ParkFramework/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-05-04T22:30:26Z","updated_at":"2016-05-05T09:14:35Z","closed_at":null,"body":"The use of callbacks, there are disadvantages\r\n\r\n1. You can not catch exceptions\r\n2. It is difficult to controls (is progress, is done, cancel) the current state executing query\r\n3. Not API to return value\r\n\r\nCallback Hell :)\r\nhttp://callbackhell.com/\r\n\r\nIn other languages, it is easy to solve, `async/await` - `$result = await $db->exec($sql)`, when there will be this in PHP is not known.\r\n\r\nHere's what I suggest, is the draft, but it works:\r\n```php\r\nclass PrototypeAsync extends pq\\Connection\r\n{\r\n\tpublic function execAsync($query, $callable = null)\r\n\t{\r\n\t\tif(is_string($query)) // for backward compatibility\r\n\t\t{\r\n\t\t\treturn parent::execAsync($query);\r\n\t\t}\r\n\r\n\t\t$coroutine = $query();\r\n\r\n\t\twhile($coroutine->valid())\r\n\t\t{\r\n\t\t\t$sql = $coroutine->current();\r\n\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\t$result = parent::exec($sql); // Must be in reality - async work in PQ\r\n\r\n\t\t\t\t$coroutine->send($result);\r\n\t\t\t}\r\n\t\t\tcatch(Throwable $e)\r\n\t\t\t{\r\n\t\t\t\t$coroutine->throw($e);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn $coroutine;\r\n\t}\r\n}\r\n\r\n//------------ User Land ----------------\r\n\r\n$db = new PrototypeAsync($dsn);\r\n\r\n$coroutine = $db->execAsync(function()\r\n{\r\n\treturn (yield 'SELECT 1')->fetchRow();\r\n});\r\n\r\nif($coroutine->valid()) // is true - executing in progress\r\n{\r\n\t// await in EventLoop\r\n\t//....\r\n\t// or cancel\r\n\t// $coroutine->throw(new Exception);\r\n}\r\nelse // is executed, get result\r\n{\r\n\t$coroutine->getReturn(); // Array([0] => 1)\r\n}\r\n\r\n// Full example - three queries and catching exceptions\r\n$coroutine = $db->execAsync(function()\r\n{\r\n\t(yield 'SELECT 1')->fetchCol($one);\r\n\r\n\techo \"Result: $one\\r\\n\";\r\n\r\n\ttry\r\n\t{\r\n\t\t(yield 'SELECT THROW')->fetchCol($value);\r\n\t}\r\n\tcatch(Throwable $e)\r\n\t{\r\n\t\techo 'Catch error: '.$e->getCode().\"\\r\\n\";\r\n\t}\r\n\r\n\t(yield 'SELECT 2')->fetchCol($two);\r\n\r\n\techo \"Result: $two\\r\\n\";\r\n\r\n\treturn $one + $two;\r\n});\r\n\r\n$ret = $coroutine->getReturn();\r\n\r\necho \"Return: $ret\\r\\n\";\r\n```\r\n\r\nPrint\r\n```\r\nResult: 1\r\nCatch error: 8\r\nResult: 2\r\nReturn: 3\r\n```\r\nWhat do you think about the use coroutine?\r\n\r\nThis does not solve the problem of independent [queries queue](https://github.com/m6w6/ext-pq/issues/10), this for only related queries or single query.\r\n\r\nThank.\r\n"},"comment":{"url":"https://api.github.com/repos/m6w6/ext-pq/issues/comments/217109332","html_url":"https://github.com/m6w6/ext-pq/issues/12#issuecomment-217109332","issue_url":"https://api.github.com/repos/m6w6/ext-pq/issues/12","id":217109332,"user":{"login":"ParkFramework","id":6284130,"avatar_url":"https://avatars.githubusercontent.com/u/6284130?v=3","gravatar_id":"","url":"https://api.github.com/users/ParkFramework","html_url":"https://github.com/ParkFramework","followers_url":"https://api.github.com/users/ParkFramework/followers","following_url":"https://api.github.com/users/ParkFramework/following{/other_user}","gists_url":"https://api.github.com/users/ParkFramework/gists{/gist_id}","starred_url":"https://api.github.com/users/ParkFramework/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ParkFramework/subscriptions","organizations_url":"https://api.github.com/users/ParkFramework/orgs","repos_url":"https://api.github.com/users/ParkFramework/repos","events_url":"https://api.github.com/users/ParkFramework/events{/privacy}","received_events_url":"https://api.github.com/users/ParkFramework/received_events","type":"User","site_admin":false},"created_at":"2016-05-05T09:14:35Z","updated_at":"2016-05-05T09:14:35Z","body":"We use a simpler API - `$result = yield $db->execAsync($sql)`, but it requires an asynchronous PHP framework.\r\n\r\nHere is an example\r\nhttps://github.com/amphp/mysql"}}