最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ES6 node asyncawait unexpected identifier - Stack Overflow

programmeradmin8浏览0评论

I have the following code that worked when running against babel. Now that I'm using harmony I get the following error:

let adResult = await ad.isUserValid(domainPath, password);
^^
SyntaxError: Unexpected identifier

The following class function:

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;

I use the class as follows:

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

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

I run the code using the following parameters:

node --harmony --use_strict --harmony-async-await user.js <my parameters>

If I take the await when calling the method:

const adResult = ad.isUserValid(domainPath, password);

then I don't have the error but it also doesnt wait till the method finishes. I've googled the error and it seems like your only able to use await within a function that async is in. But without await outside of the method call, it doesnt wait till its finished. Any ideas?

I have the following code that worked when running against babel. Now that I'm using harmony I get the following error:

let adResult = await ad.isUserValid(domainPath, password);
^^
SyntaxError: Unexpected identifier

The following class function:

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;

I use the class as follows:

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

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

I run the code using the following parameters:

node --harmony --use_strict --harmony-async-await user.js <my parameters>

If I take the await when calling the method:

const adResult = ad.isUserValid(domainPath, password);

then I don't have the error but it also doesnt wait till the method finishes. I've googled the error and it seems like your only able to use await within a function that async is in. But without await outside of the method call, it doesnt wait till its finished. Any ideas?

Share Improve this question edited Nov 15, 2017 at 20:44 Michał Perłakowski 92.8k30 gold badges163 silver badges187 bronze badges asked Feb 2, 2017 at 3:02 advineradviner 3,59710 gold badges38 silver badges70 bronze badges 4
  • 1 which version of node are you using? – Sridhar Commented Feb 2, 2017 at 4:53
  • It sounds like you're using an old version of Node. Any version of Node 4.3 onwards should support the let keyword. – Aron Commented Feb 2, 2017 at 9:11
  • You guys are right I switched puters that I thought had the latest version 7. Once I updated it it worked. Thanks for the tip – adviner Commented Feb 2, 2017 at 14:29
  • Possible duplicate of 'await Unexpected identifier' on Node.js 7.5 – Michał Perłakowski Commented Nov 15, 2017 at 20:44
Add a ment  | 

1 Answer 1

Reset to default 5

It is because you can't use await unless it's in an async function.

See this link for details:

'await Unexpected identifier' on Node.js 7.5

发布评论

评论列表(0)

  1. 暂无评论