admin管理员组

文章数量:1168530

I'm currently trying to use async/await for a function that requires the loop to be synchronous.

This is the function:

async channelList(resolve, reject) {
    let query = ['channellist'].join(' ');

    this.query.exec(query)
    .then(response => {
        let channelsRaw = response[0].split('|');
        let channels = [];

        channelsRaw.forEach(data => {
            let dataParsed = ResponseParser.parseLine(data);

            let method = new ChannelInfoMethod(this.query);
            let channel = await method.run(dataParsed.cid);

            channels.push(channel);
        });

        resolve(channels);
    })
    .catch(error => reject(error));
}

When I try to run it, I get this error:

let channel = await method.run(dataParsed.cid);
                    ^^^^^^
SyntaxError: Unexpected identifier

What could be the cause of it?
Thanks!

I'm currently trying to use async/await for a function that requires the loop to be synchronous.

This is the function:

async channelList(resolve, reject) {
    let query = ['channellist'].join(' ');

    this.query.exec(query)
    .then(response => {
        let channelsRaw = response[0].split('|');
        let channels = [];

        channelsRaw.forEach(data => {
            let dataParsed = ResponseParser.parseLine(data);

            let method = new ChannelInfoMethod(this.query);
            let channel = await method.run(dataParsed.cid);

            channels.push(channel);
        });

        resolve(channels);
    })
    .catch(error => reject(error));
}

When I try to run it, I get this error:

let channel = await method.run(dataParsed.cid);
                    ^^^^^^
SyntaxError: Unexpected identifier

What could be the cause of it?
Thanks!

Share Improve this question edited Apr 11, 2017 at 23:44 Felix Kling 816k180 gold badges1.1k silver badges1.2k bronze badges asked Apr 7, 2017 at 23:44 Ron MelkhiorRon Melkhior 4752 gold badges4 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 51

Your async is defined on channelList and not on the arrow function where the await is contained. Move async to that arrow function:

channelsRaw.forEach(async (data) => {
    let dataParsed = ResponseParser.parseLine(data);

    let method = new ChannelInfoMethod(this.query);
    let channel = await method.run(dataParsed.cid);

    channels.push(channel);
});

Also, since you're using async anyways, you can just async the entire promise chain you have there.

本文标签: javascriptUnexpected identifier when using awaitStack Overflow