File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ module.exports = async function (req, res) {
3030 const currentViews = db . has ( pathname ) ? db . get ( pathname ) . views . length : 0
3131 // Add a view and send the total views back to the client
3232 if ( shouldIncrement ) {
33- pushView ( pathname , { time : Date . now ( ) } )
33+ await pushView ( pathname , { time : Date . now ( ) } )
3434 }
3535 if ( req . method === 'GET' ) {
3636 send ( res , 200 , { views : shouldIncrement ? currentViews + 1 : currentViews } )
Original file line number Diff line number Diff line change @@ -4,10 +4,10 @@ const db = require('./db')
44// db messing up.
55const pushView = async ( key , view ) => {
66 const locks = { }
7- push ( )
7+ await push ( )
88
9- function push ( ) {
10- if ( locks [ key ] ) return setImmediate ( push )
9+ async function push ( ) {
10+ if ( locks [ key ] ) return setImmediate ( async ( ) => { await push ( ) } )
1111 locks [ key ] = true
1212
1313 let views
@@ -18,11 +18,8 @@ const pushView = async (key, view) => {
1818 }
1919
2020 try {
21- db . put ( key , { views : views . concat ( [ view ] ) } )
22- . then ( ( ) => {
23- delete locks [ key ]
24- } )
25- . catch ( err => { throw err } )
21+ await db . put ( key , { views : views . concat ( [ view ] ) } )
22+ delete locks [ key ]
2623 } catch ( err ) {
2724 delete locks [ key ]
2825 throw err
You can’t perform that action at this time.
0 commit comments