Reputation: 569
I have problem with Babel 5.8.38 and await in Node.js My code seems like that:
/**
* Imports
*/
import {signature} from '../core/antpool';
import log from '../core/logging';
import config from '../config';
import {sendLog as sendEmailLog, EmailTemplate} from '../core/email';
import {rethinkdb, Decorators as DBDecorators} from '../core/db';
import request from 'request';
const tables = {
AntpoolAccount: 'CronAntpoolAccount'
};
class Antpool {
@DBDecorators.table(tables.AntpoolAccount)
static async account(coin) {
var nonce = Date.now();
request.post({
url: config.antpool.baseUrl + '/api/account.htm',
json: true,
form: {
key: config.antpool.key,
nonce: nonce,
signature: signature(nonce),
coin: coin
}
}, function (err, httpResponse, body) {
if (err || httpResponse.statusCode != 201) {
log.error(err, '[CRON][Antpool][Account] Connection problem');
sendEmailLog(EmailTemplate.LOG, {
message: '[CRON][Antpool][Account] Connection problem'
});
return false;
}
var out = JSON.parse(body);
if (out.code != 0) {
log.error(err, '[CRON][Antpool][Account] Error response('+out.code+'): '+out.message);
sendEmailLog(EmailTemplate.LOG, {
message: '[CRON][Antpool][Account] Error response('+out.code+'): '+out.message
});
return false;
}
// Add to database
let obj = {
earn24: out.data.earn24Hours,
earnTot: out.data.earnTotal,
paidOut: out.data.paidOut,
balance: out.data.balance,
createdAt: new Date()
};
// Insert into database
let insert = await this.table.insert(obj).run();
if(insert) {
return true;
} else {
return false;
}
});
}
}
/**
* Exports
*/
export {Antpool};
What I get is only error which have problem with await.
SyntaxError: .../antpool.js: Unexpected token (59:22)
// Insert into database
let insert = await this.table.insert(obj).run();
I am thinking what can be solution for accept the await. Is it little weird because in other parts of code await works well. Not sure what the problem exactly is, but spent about two days finding the issue.
I am calling scripts with:
/**
* Automatically hook babel into all node requires.
*/
require('babel/register')({
optional: ['es7.asyncFunctions', 'es7.classProperties', 'es7.decorators']
});
/**
* Start application worker.
*/
require('./src/worker');
Any help is very appreciated.
Upvotes: 0
Views: 223
Reputation: 661
The keyword await
can be used only in functions that are marked as async
. Function in which you are trying to use await
:
, function (err, httpResponse, body) { ...
is not marked as async
and therefore it can't be compiled.
More info: MDN article on async/await
Upvotes: 2