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!
1 Answer
Reset to default 51Your 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
版权声明:本文标题:javascript - Unexpected identifier when using await - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737594882a1998068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论