ES6节点异步/等待意外标识符

adv*_*ner 3 javascript async-await es6-promise

我有以下代码在运行对抗babel时有效.现在我正在使用和声,我收到以下错误:

let adResult = await ad.isUserValid(domainPath, password);
^^
SyntaxError: Unexpected identifier
Run Code Online (Sandbox Code Playgroud)

以下类函数:

class ActiveDirectoryHelper {
    constructor(options) {
        this.config = options.config
        this.ad = null;
    }

    connect() {

        var config = {
            url: this.config.url,
            baseDN: this.config.baseDN,
            attributes: this.config.attributes
        };

        if (this.config.account.user.length > 0) {
            config.username = this.config.account.user;
            config.password = this.config.account.password;
        }

        this.ad = new ActiveDirectory(config);
    }

    async isUserValid(user, password) {
        return new Promise((resolve, reject) => {

            this.ad.authenticate(user, password, (err, auth) => {
                if (err) {
                    reject({
                        code: 500,
                        message: "Unknown authentication error",
                        entry: {}
                    });
                }

                if (auth) {
                    resolve({
                        code: 200,
                        message: "OK",
                        entry: {
                            user: user,
                            password: password
                        }
                    });

                } else {
                    reject({
                        code: 400,
                        message: "Authentication failed",
                        entry: {}
                    });

                }

            });
        });
    }
...

exports.ActiveDirectoryHelper = ActiveDirectoryHelper;
Run Code Online (Sandbox Code Playgroud)

我使用如下类:

const ad = new ActiveDirectoryHelper({
    config: adConfig
});
ad.connect();

const domainPath = domain.length > 0 ? `${domain}\\${user}` : user;
const adResult = await ad.isUserValid(domainPath, password);
Run Code Online (Sandbox Code Playgroud)

我使用以下参数运行代码:

node --harmony --use_strict --harmony-async-await user.js <my parameters>
Run Code Online (Sandbox Code Playgroud)

如果我在调用方法时采取等待:

const adResult = ad.isUserValid(domainPath, password);
Run Code Online (Sandbox Code Playgroud)

然后我没有错误但它也没有等到方法完成.我已经google了错误,看起来你只能在async所在的函数中使用await.但是如果没有等待方法调用之外,它就不会等到它完成了.有任何想法吗?

Sam*_*raf 5

这是因为你不能使用await它,除非它在一个async函数中.

有关详情,请参阅此链接:

在Node.js 7.5上"等待意外的标识符"