Recently, I found a restify 6.4.0 use chain that used to function properly, blocking at the point of calling next(), not proceeding to the next handler. After much debugging, I am able to reproduce it simply by calling an async function.
module.exports = function (server,redisClient) {
server.use([
(req, res, next) => {
util.log('I am first in chain!')
next()
},
(req, res, next) => {
util.log(`Restify request: ${req.url}`)
createOrVerifySession(req,res,redisClient).then(success => {
if(success) {
console.log('I have successfully created or verified the session.')
next()
}
else {
console.log('I have failed to create or verify the session.')
next(false)
}
})
},
(req, res, next) => {
util.log('I am next in chain!')
next()
}
])
}
async function createOrVerifySession(req,res,redisClient) {
return true
}
However, if a non-async function is called. The chain proceeds
module.exports = function (server,redisClient) {
server.use([
(req, res, next) => {
util.log('I am first in chain!')
next()
},
(req, res, next) => {
util.log(`Restify request: ${req.url}`)
let success = createOrVerifySession(req,res,redisClient)
if(success) {
console.log('I have successfully created or verified the session.')
next()
}
else {
console.log('I have failed to create or verify the session.')
next(false)
}
})
},
(req, res, next) => {
util.log('I am next in chain!')
next()
}
])
}
function createOrVerifySession(req,res,redisClient) {
return true
}
Unfortunately, I need the actual createOrVerifySessions function to be asyncronous, as it uses await to invoke asyncronous calls to redisClient.
Does anyone know why the asyncronous call blocks the chain, or at least how to debug into a next() chain? TIA for any assistance.
By replacing the body of createOrVerifySession with a simple return true, I was able to confirm that just the call to the asyncronous function, which proceeded into the then(f => {}) function, did not proceed to the next handler in the chain when next() was called, but if changed to a non-asyncronous function, it proceeded to the next handler.