【问题标题】:Babel 5.8.38 ignore await in Node.jsBabel 5.8.38 忽略 Node.js 中的等待
【发布时间】:2017-11-11 08:42:56
【问题描述】:

我对 Babel 5.8.38 有问题并在 Node.js 中等待 我的代码是这样的:

/**
 * 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};

我得到的只是等待有问题的错误。

SyntaxError: .../antpool.js: Unexpected token (59:22)

            // Insert into database
            let insert = await this.table.insert(obj).run();

我正在考虑接受等待的解决方案。是不是有点奇怪,因为在代码的其他部分 await 工作得很好。 不确定到底是什么问题,但花了大约两天时间才找到问题。

我正在调用脚本:

/**
 * Automatically hook babel into all node requires.
 */
require('babel/register')({
    optional: ['es7.asyncFunctions', 'es7.classProperties', 'es7.decorators']
});

/**
 * Start application worker.
 */
require('./src/worker');

非常感谢任何帮助。

【问题讨论】:

  • 您在未签名为异步的函数“function (err, httpResponse, body)”中使用 await

标签: node.js async-await babeljs


【解决方案1】:

关键字await 只能用于标记为async 的函数中。您尝试使用的函数await

, function (err, httpResponse, body) { ...

未标记为async,因此无法编译。

更多信息:MDN article on async/await

【讨论】:

  • 下一个问题来了:执行:_cronsAntpool2['default'].account('btc') 无法读取未定义的属性 'account' 这似乎是 hapi-cron-job 和组合的问题:执行: Antpool.account('btc')
  • 这似乎与您发布的代码无关。但是,某些函数的异步回调似乎有些问题。当您意识到调用了哪个函数时,您可能会意识到它的this 设置为undefined。然后,您需要找到传递该回调的位置,并将其绑定到带有.bind(concreteObject) 的具体对象。更多关于.bind()developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
猜你喜欢
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多