{"action":"created","issue":{"url":"https://api.github.com/repos/brianc/node-pg-native/issues/16","labels_url":"https://api.github.com/repos/brianc/node-pg-native/issues/16/labels{/name}","comments_url":"https://api.github.com/repos/brianc/node-pg-native/issues/16/comments","events_url":"https://api.github.com/repos/brianc/node-pg-native/issues/16/events","html_url":"https://github.com/brianc/node-pg-native/issues/16","id":67797844,"number":16,"title":"query function breaks Promises","user":{"login":"trailsandtribulations","id":842707,"avatar_url":"https://avatars.githubusercontent.com/u/842707?v=3","gravatar_id":"","url":"https://api.github.com/users/trailsandtribulations","html_url":"https://github.com/trailsandtribulations","followers_url":"https://api.github.com/users/trailsandtribulations/followers","following_url":"https://api.github.com/users/trailsandtribulations/following{/other_user}","gists_url":"https://api.github.com/users/trailsandtribulations/gists{/gist_id}","starred_url":"https://api.github.com/users/trailsandtribulations/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trailsandtribulations/subscriptions","organizations_url":"https://api.github.com/users/trailsandtribulations/orgs","repos_url":"https://api.github.com/users/trailsandtribulations/repos","events_url":"https://api.github.com/users/trailsandtribulations/events{/privacy}","received_events_url":"https://api.github.com/users/trailsandtribulations/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":11,"created_at":"2015-04-11T15:11:04Z","updated_at":"2015-06-06T07:18:11Z","closed_at":null,"body":"Promises work great on regular `pg`. however, in `pg-native`, a resolve from a `connect` works fine, but resolve from `query` for some reason does not work at all.\r\n\r\nhere is the simplest test case ( using `node --harmony --use_strict test.js`):\r\n\r\n<pre>\r\n// test.js\r\n\r\n'use strict';\r\n\r\nconst Client = require('pg-native')\r\n\r\nexports.testQuery = function(cl) {\r\n  return new Promise( (resolve,reject) => {\r\n    cl.query( 'select 1 as col', (err,rows) => {\r\n      if( err ) return reject( err );\r\n      console.log( 'selected '+rows[0].col );\r\n      // resolve is called, but does not get to the then()\r\n      resolve( rows[0].col );\r\n    });\r\n  });\r\n}\r\nexports.testing = function() {\r\n  console.log( 'testing' );\r\n  let client = new Client();\r\n  client.connect( 'port = 6432 dbname = react', err => {\r\n    if( err ) return console.log( 'conn err: '+err );\r\n    console.log( 'connected' );\r\n    exports.testQuery(client)\r\n    .then( x => console.log('resolved x='+x) )\r\n    .catch( x => console.log( 'err='+x ) ); \r\n   });\r\n}\r\n\r\nexports.testing();\r\n</pre>"},"comment":{"url":"https://api.github.com/repos/brianc/node-pg-native/issues/comments/109542441","html_url":"https://github.com/brianc/node-pg-native/issues/16#issuecomment-109542441","issue_url":"https://api.github.com/repos/brianc/node-pg-native/issues/16","id":109542441,"user":{"login":"trailsandtribulations","id":842707,"avatar_url":"https://avatars.githubusercontent.com/u/842707?v=3","gravatar_id":"","url":"https://api.github.com/users/trailsandtribulations","html_url":"https://github.com/trailsandtribulations","followers_url":"https://api.github.com/users/trailsandtribulations/followers","following_url":"https://api.github.com/users/trailsandtribulations/following{/other_user}","gists_url":"https://api.github.com/users/trailsandtribulations/gists{/gist_id}","starred_url":"https://api.github.com/users/trailsandtribulations/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trailsandtribulations/subscriptions","organizations_url":"https://api.github.com/users/trailsandtribulations/orgs","repos_url":"https://api.github.com/users/trailsandtribulations/repos","events_url":"https://api.github.com/users/trailsandtribulations/events{/privacy}","received_events_url":"https://api.github.com/users/trailsandtribulations/received_events","type":"User","site_admin":false},"created_at":"2015-06-06T07:18:11Z","updated_at":"2015-06-06T07:18:11Z","body":"@brianc - have not tried with other Promise systems - running `node --harmony --use_strict`. assume that, in the end, this stuff will work.\r\n\r\nagree that `setTimeout()` is epic hack. but in the end, once all working, will revert to sanity.\r\n\r\nhere's the `consume()` - this is not to say `startReader()` etc was impeccable to begin with:\r\n\r\n<pre>\r\nPrPq.prototype._consume = () => {\r\n  let pq = this;\r\n\r\n  // if not busy, nothing to consume\r\n  if( !pq._conn.libpq.isBusy() ) return Promise.resolve( pq );\r\n\r\n  // KLUDGE\r\n  return new Promise( (resolve,reject) => {\r\n    function consume() {\r\n      if( !pq._conn.libpq.consumeInput() ) return reject( pq._conn.libpq.errorMessage() );\r\n      if( !pq._conn.libpq.isBusy() ) return resolve( pq );\r\n      setTimeout( consume );\r\n    }\r\n    consume();\r\n  } );\r\n  //\r\n\r\n  /* startReader() to listen to 'readable' events\r\n  return new Promise( (resolve,reject) => {\r\n    pq._conn.libpq.startReader();\r\n    pq._conn.libpq.on('readable', function() {\r\n\r\n      // consume input\r\n      if( !pq._conn.libpq.consumeInput() ) return reject( pq._conn.libpq.errorMessage() );\r\n\r\n      // note: consuming a 2nd buffer of input later...\r\n      if(pq._conn.libpq.isBusy()) return;\r\n     \r\n      // release listener, stop watcher \r\n      //~pq._conn.libpq.removeListener('readable', onReadable);\r\n      pq._conn.libpq.removeAllListeners('readable');\r\n      pq._conn.libpq.stopReader();\r\n\r\n      // done\r\n      resolve( pq );\r\n    });\r\n  });\r\n  */\r\n}\r\n</pre>\r\n"}}