Sunday, 30 April 2017

ES7 Async/Await in Express routes

I'm currently using Express with routes in classes and would like to use async/await in the handlers for routes. Currently I've this:

class Routes {
        constructor(){
        }

        Tags = (req, res, next) => 
                this.myAsyncFunction (req.body).then((result) => { res.json(result) });
        }
}

Now I'd like to use the async/await functionnality to not use then() anymore and I've tried this:

class Routes {
        constructor(){
        }

        Tags = async (req, res, next) => 
                result = await this.myAsyncFunction (req.body);
                res.json(result);
        }
}

The code transpiles well in babel and shows not error when launched but it looks like it hangs waiting for something in the route handler. Any hint please ?



via Sebastien Cantos

No comments:

Post a Comment