1414 * limitations under the License.
1515 */
1616
17- const util = require ( 'util ' ) ;
17+ const async = require ( 'async ' ) ;
1818const fs = require ( 'fs' ) ;
19+ const util = require ( 'util' ) ;
1920const BigQuery = require ( '../src/index.js' ) ;
2021
2122if ( process . argv . length < 3 ) {
@@ -25,7 +26,7 @@ var queryJson = fs.readFileSync(process.argv[2]);
2526var queries = JSON . parse ( queryJson ) ;
2627var client = BigQuery ( ) ;
2728
28- var doQuery = function ( queryTxt ) {
29+ var doQuery = function ( queryTxt , callback ) {
2930 var startMilli = new Date ( ) . getTime ( ) ;
3031 var numRows = 0 ;
3132 var numCols ;
@@ -38,31 +39,31 @@ var doQuery = function(queryTxt) {
3839
3940 client . createQueryStream ( query )
4041 . on ( 'error' , function ( err ) {
41- throw err ;
42+ callback ( err )
4243 } )
4344 . on ( 'data' , function ( row ) {
4445 if ( numRows == 0 ) {
4546 numCols = Object . keys ( row ) . length ;
4647 timeFirstByteMilli = new Date ( ) . getTime ( ) - startMilli ;
4748 } else if ( numCols != Object . keys ( row ) . length ) {
48- throw util . format ( "query %j: wrong number of columns, want %d got %d" ,
49+ this . end ( ) ;
50+ callback ( util . format ( "query %j: wrong number of columns, want %d got %d" ,
4951 queryTxt ,
5052 numCols ,
51- Object . keys ( row ) . length ) ;
53+ Object . keys ( row ) . length ) ) ;
5254 }
5355 numRows ++ ;
5456 } )
5557 . on ( 'end' , function ( ) {
5658 timeTotalMilli = new Date ( ) . getTime ( ) - startMilli ;
5759 console . log ( util . format ( "query %j: got %d rows, %d cols, first byte %d sec, total %d sec" ,
58- queryTxt ,
59- numRows ,
60- numCols ,
61- timeFirstByteMilli / 1000 ,
62- timeTotalMilli / 1000 ) ) ;
60+ queryTxt ,
61+ numRows ,
62+ numCols ,
63+ timeFirstByteMilli / 1000 ,
64+ timeTotalMilli / 1000 ) ) ;
65+ callback ( null ) ;
6366 } ) ;
6467}
6568
66- for ( queryTxt of queries ) {
67- doQuery ( queryTxt ) ;
68- }
69+ async . eachSeries ( queries , doQuery , err => { } ) ;
0 commit comments