@@ -349,19 +349,37 @@ Client.prototype.copyTo = function (text) {
349349} ;
350350
351351Client . prototype . query = function ( config , values , callback ) {
352- //can take in strings, config object or query object
353- var query = ( typeof config . submit == 'function' ) ? config :
354- new Query ( config , values , callback ) ;
352+ var promise ;
353+ var isQueryable = typeof config . submit == 'function' ;
354+ var query ;
355+ // if we receive an object with a 'submit' function we delegate
356+ // processing to the passed object - this is how pg.Query, QueryStream, and Cursor work
357+ if ( isQueryable ) {
358+ query = config ;
359+ } else {
360+ query = new Query ( config , values , callback ) ;
361+ if ( ! query . callback ) {
362+ promise = new global . Promise ( function ( resolve , reject ) {
363+ query . on ( 'error' , reject ) ;
364+ query . on ( 'end' , resolve ) ;
365+ } ) ;
366+ }
367+ }
355368 if ( this . binary && ! query . binary ) {
356369 query . binary = true ;
357370 }
371+
372+ // TODO - this is a smell
358373 if ( query . _result ) {
359374 query . _result . _getTypeParser = this . _types . getTypeParser . bind ( this . _types ) ;
360375 }
361376
362377 this . queryQueue . push ( query ) ;
363378 this . _pulseQueryQueue ( ) ;
364- return query ;
379+
380+ // if we were passed a queryable, return it
381+ // otherwise return callback/promise result
382+ return isQueryable ? query : promise ;
365383} ;
366384
367385Client . prototype . end = function ( cb ) {
0 commit comments