I have an express route, say:
app.use('/route', middleware, handler);
function middleware(req, res, next) {
    // do some async task
    if( success ) {
        // async task is successfully done. handler should be called
        next();
    } else {
        // error in async task, handler should not be called
        res.json({ message: 'fail'});
    }
}
handler(req, res, next) {
    res.json({ message: 'done'});
}
I want to do some task asynchronously and if that task is done successfully, then and then only the subsequent middlewares should be called.
The issue is before the asynchronous task is finished, the handler is called (as expected) and the response is ended.
Thus, when the async task is finished and I try to call res.json() in middleware it gives me 'Can't set headers after they are sent' (as expceted)
So how do I make express wait in the middleware while it is doing async task such that once it is finished, only and then only the subsequent handlers should get called.
I have looked at (here) 
But not much of help. 
I have tried using req.pause() but that does not seem to work.
 
    