adviner
adviner

Reputation: 3567

ES6 node async/await unexpected identifier

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?

Upvotes: 4

Views: 11416

Answers (1)

Sameh Sharaf
Sameh Sharaf

Reputation: 1141

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

Upvotes: 5

Related Questions