Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6节点异步/等待意外标识符_Javascript_Async Await_Es6 Promise - Fatal编程技术网

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

Javascript ES6节点异步/等待意外标识符,javascript,async-await,es6-promise,Javascript,Async Await,Es6 Promise,我有下面的代码,在运行babel时有效。现在我正在使用harmony,出现以下错误: let adResult = await ad.isUserValid(domainPath, password); ^^ SyntaxError: Unexpected identifier 下面的类函数: class ActiveDirectoryHelper { constructor(options) { this.config = options.config

我有下面的代码,在运行babel时有效。现在我正在使用harmony,出现以下错误:

let adResult = await ad.isUserValid(domainPath, password);
^^
SyntaxError: Unexpected identifier
下面的类函数:

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;
我使用的类如下所示:

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

const domainPath = domain.length > 0 ? `${domain}\\${user}` : user;
const adResult = await ad.isUserValid(domainPath, password);
我使用以下参数运行代码:

node --harmony --use_strict --harmony-async-await user.js <my parameters>
然后我没有错误,但它也不会等到方法完成。
我在谷歌上搜索了错误,似乎您只能在async所在的函数中使用Wait。但是如果在方法调用之外没有wait,它就不会等到调用完成。有什么想法吗?

这是因为你不能使用
等待
,除非它是在
异步
函数中

有关详细信息,请参阅此链接:


您使用的是哪个版本的
node
?听起来您使用的是旧版本的node。Node 4.3以后的任何版本都应该支持let关键字。你们说得对,我换了一些我认为最新版本7的计算机。一旦我更新了它,它就工作了。感谢您提供的提示副本。我们在异步函数中得到了此错误,但无法找出它。我在
array.forEach(item=>{x=await…})
call中有一个wait,当然这是一个函数,所以我也必须使它异步:
array.forEach(async item=>{x=await…})
const adResult = ad.isUserValid(domainPath, password);