@@ -99,34 +99,31 @@ app.on('listening', async () => {
9999// Serve the index page, showing vote tallies.
100100app . get ( '/' , async ( req , res ) => {
101101 // Get the 5 most recent votes.
102- const recentResultPromise = pool
103- . query (
104- 'SELECT candidate, time_cast FROM votes ORDER BY time_cast DESC LIMIT 5'
105- )
106- . then ( rows => {
107- return rows ;
108- } ) ;
102+ const recentVotesQuery = pool . query (
103+ 'SELECT candidate, time_cast FROM votes ORDER BY time_cast DESC LIMIT 5'
104+ ) ;
109105
106+ // Get votes
110107 const stmt = 'SELECT COUNT(vote_id) as count FROM votes WHERE candidate=?' ;
111- // Get the total number of " TABS" votes.
112- const tabsResultPromise = pool . query ( stmt , [ 'TABS ' ] ) . then ( rows => {
113- return rows [ 0 ] . count ;
114- } ) ;
115- // Get the total number of "SPACES" votes.
116- const spacesResultPromise = pool . query ( stmt , [ 'SPACES' ] ) . then ( rows => {
117- return rows [ 0 ] . count ;
118- } ) ;
108+ const tabsQuery = pool . query ( stmt , [ ' TABS' ] ) ;
109+ const spacesQuery = pool . query ( stmt , [ 'SPACES ' ] ) ;
110+
111+ // Run queries concurrently, and wait for them to complete
112+ // This is faster than await-ing each query object as it is created
113+ const recentVotes = await recentVotesQuery ;
114+ const [ tabsVotes ] = await tabsQuery ;
115+ const [ spacesVotes ] = await spacesQuery ;
119116
120117 res . render ( 'index.pug' , {
121- recentVotes : await recentResultPromise ,
122- tabCount : await tabsResultPromise ,
123- spaceCount : await spacesResultPromise ,
118+ recentVotes,
119+ tabCount : tabsVotes . count ,
120+ spaceCount : spacesVotes . count ,
124121 } ) ;
125122} ) ;
126123
127124// Handle incoming vote requests and inserting them into the database.
128125app . post ( '/' , async ( req , res ) => {
129- const team = req . body . team ;
126+ const { team} = req . body ;
130127 const timestamp = new Date ( ) ;
131128
132129 if ( ! team || ( team !== 'TABS' && team !== 'SPACES' ) ) {
0 commit comments