{"action":"created","issue":{"url":"https://api.github.com/repos/felixge/node-mysql/issues/929","labels_url":"https://api.github.com/repos/felixge/node-mysql/issues/929/labels{/name}","comments_url":"https://api.github.com/repos/felixge/node-mysql/issues/929/comments","events_url":"https://api.github.com/repos/felixge/node-mysql/issues/929/events","html_url":"https://github.com/felixge/node-mysql/issues/929","id":46556844,"number":929,"title":"Using node-mysql with promises","user":{"login":"chrisveness","id":1066395,"avatar_url":"https://avatars.githubusercontent.com/u/1066395?v=3","gravatar_id":"","url":"https://api.github.com/users/chrisveness","html_url":"https://github.com/chrisveness","followers_url":"https://api.github.com/users/chrisveness/followers","following_url":"https://api.github.com/users/chrisveness/following{/other_user}","gists_url":"https://api.github.com/users/chrisveness/gists{/gist_id}","starred_url":"https://api.github.com/users/chrisveness/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrisveness/subscriptions","organizations_url":"https://api.github.com/users/chrisveness/orgs","repos_url":"https://api.github.com/users/chrisveness/repos","events_url":"https://api.github.com/users/chrisveness/events{/privacy}","received_events_url":"https://api.github.com/users/chrisveness/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/felixge/node-mysql/labels/question","name":"question","color":"d4c5f9"}],"state":"open","locked":false,"assignee":{"login":"dougwilson","id":67512,"avatar_url":"https://avatars.githubusercontent.com/u/67512?v=3","gravatar_id":"","url":"https://api.github.com/users/dougwilson","html_url":"https://github.com/dougwilson","followers_url":"https://api.github.com/users/dougwilson/followers","following_url":"https://api.github.com/users/dougwilson/following{/other_user}","gists_url":"https://api.github.com/users/dougwilson/gists{/gist_id}","starred_url":"https://api.github.com/users/dougwilson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougwilson/subscriptions","organizations_url":"https://api.github.com/users/dougwilson/orgs","repos_url":"https://api.github.com/users/dougwilson/repos","events_url":"https://api.github.com/users/dougwilson/events{/privacy}","received_events_url":"https://api.github.com/users/dougwilson/received_events","type":"User","site_admin":false},"milestone":null,"comments":30,"created_at":"2014-10-22T20:31:52Z","updated_at":"2015-04-21T13:46:37Z","closed_at":null,"body":"Can node-mysql be used with promises, or only with callbacks? If it can't, can you suggest how best to use it within a promises structure? (I'm a bit lost with promisify, bluebird, Q, ...)"},"comment":{"url":"https://api.github.com/repos/felixge/node-mysql/issues/comments/94797675","html_url":"https://github.com/felixge/node-mysql/issues/929#issuecomment-94797675","issue_url":"https://api.github.com/repos/felixge/node-mysql/issues/929","id":94797675,"user":{"login":"bucaran","id":8317250,"avatar_url":"https://avatars.githubusercontent.com/u/8317250?v=3","gravatar_id":"","url":"https://api.github.com/users/bucaran","html_url":"https://github.com/bucaran","followers_url":"https://api.github.com/users/bucaran/followers","following_url":"https://api.github.com/users/bucaran/following{/other_user}","gists_url":"https://api.github.com/users/bucaran/gists{/gist_id}","starred_url":"https://api.github.com/users/bucaran/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bucaran/subscriptions","organizations_url":"https://api.github.com/users/bucaran/orgs","repos_url":"https://api.github.com/users/bucaran/repos","events_url":"https://api.github.com/users/bucaran/events{/privacy}","received_events_url":"https://api.github.com/users/bucaran/received_events","type":"User","site_admin":false},"created_at":"2015-04-21T13:46:37Z","updated_at":"2015-04-21T13:46:37Z","body":"Just in case someone is wondering how to build an example without `promisifyAll` (as I couldn't really make it work exactly like I wanted and you know people have to work also...)\r\n\r\nThis is a ES7-like example of how things would go:\r\n\r\n```js\r\n\r\nlet mysql = require(\"mysql\")\r\n\r\nlet database_query = function (options, query) {\r\n return new Promise(function(resolve, reject) {\r\n  let {host, user, password, database} = options\r\n  let conn = mysql.createConnection({\r\n    host     : host,\r\n    user     : user,\r\n    password : password,\r\n    database : database\r\n  })\r\n  conn.connect()\r\n  conn.query(query, function(err, data) {\r\n    return (err ? reject(err) : resolve(data));\r\n  });\r\n  conn.end()\r\n })\r\n}\r\n\r\nlet options = {\r\n  host     : process.ENV_HOST || \"localhost\",\r\n  user     : process.ENV_USER || \"root\",\r\n  // ...\r\n}\r\n\r\nasync function () { \r\n  try {\r\n    let rows = await database_query( options, queryString )\r\n    rows.forEach((row) => row_view( row ))\r\n  } catch (err) {\r\n    handleError( err )\r\n  }\r\n}\r\n``"}}