necroface
necroface

Reputation: 3465

How to use async await as module method

This is a route I write, using Express

const express=require('express');
const router=express.Router();

const trackRepo=require('../model/track');

router.post('/live',function(req,res){
    const time=1439832167;
    const list=trackRepo.getAlerts(time);
    res.json({list:list});
});

module.exports=router;

Where I use async/await:

const r=require('rethinkdb');
const config=require('../config/rethinkdb');

r.connect(config).then((conn)=>{
    module.exports.getAlerts=async (function(time){
        const cursor=await r.table('track').filter({createdAt:time}).run(conn);
        return await cursor.toArray();
    });
});

This is my app.js

const express=require('express');
const app=express();
const server=require('http').createServer(app);
const bodyParser=require('body-parser');
const cookieParser=require('cookie-parser');
const session=require('express-session');

// Utilities
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(cookieParser());

// Template
app.set('view engine','ejs');

// Static
app.use(express.static(__dirname+'/static'));

// Middlewares
app.use(require('./route/web'));
app.use(require('./route/track'));

// Listening
app.listen(4444,function(){
    console.log('Server listening on port 4444...');
});

I write a file named server.js, this is supposed to be the entry point, transpiling app.js to babel before running server

require('babel/register');
require('./app.js');

This is list of devDependencies I install in package.json

 "devDependencies": {
    "babel-cli": "^6.16.0",
    "babel-core": "^6.17.0",
    "babel-plugin-syntax-async-functions": "^6.13.0",
    "babel-plugin-transform-async-to-generator": "^6.16.0",
    "babel-plugin-transform-regenerator": "^6.16.1",
    "babel-polyfill": "^6.16.0",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-latest": "^6.16.0"
  }

When I start server by using command babel-node server.js, it throws Unexpected token at the async await line. I follow the guides I searched and feel confused, I don't get why it keeps throwing errors. How can I fix this?

Upvotes: 1

Views: 6305

Answers (1)

Bergi
Bergi

Reputation: 664548

You're getting the exception because of the syntax error async(function(){ await … }), which calls a function named async with a function expression as the argument, a function expression that uses the await keyword despite being tagged as async.

It should be

router.post('/live', async function(req,res){
    const time = 1439832167;
    const list = await trackRepo.getAlerts(time);
//               ^^^^^ it's a promise, so you need to await it
    res.json({list:list});
});

const r = require('rethinkdb');
const config = require('../config/rethinkdb');

const connection = r.connect(config);
module.exports.getAlerts = async function(time) {
//                         ^^^^^^^^^^^^^^ two keywords, nothing between
    const conn = await connection;
//               ^^^^^^^^^^^^^^^^ don't export functions asynchronously, just wait inside it
    const cursor = await r.table('track').filter({createdAt:time}).run(conn);
    return await cursor.toArray();
};

Upvotes: 3

Related Questions