gpt4 book ai didi

javascript - 标记的 continue 语句; for循环

转载 作者:搜寻专家 更新时间:2023-11-01 00:29:46 24 4
gpt4 key购买 nike

continue connloop; 抛出 Syntax Error: Unsyntactic continue

如果我将 continue connloop; 更改为 continue; ,它将运行(但当然它不会在外部循环上执行,而是在内部循环上执行)

为什么会这样,这在 nodejs/ecma6 中是否已弃用或非法?

请不要推荐我使用函数调用代替标签。

redisSubscriber.on("message", function(channel, event){

event = JSON.parse(event);
const eventPayload = JSON.stringify(event.payload);

connloop:
for(let conn in connections){
conn = connections[conn];
redisClient.SMEMBERS('connection/'+conn.id+'/subscriptions', (err, subscriptions)=>{

let intersectedTags = [];
if(event.address.tags.length > 0 && subscriptions.length > 0){
for(let tag in subscriptions){
tag = subscriptions[tag];

for(let _tag in event.address.tags){
_tag = event.address.tags[_tag];
if(tag == _tag)
intersectedTags.push(tag);

}
}
}

let exclusive = false;
for(let userId in event.address.include){
userId = event.address.include[userId];
if(userId == conn.userId){
exclusive = true;
break;
}
}

if(intersectedTags.length > 0 || exclusive){
if(event.address.exclude){
for(let exclude in event.address.exclude){
exclude = event.address.exclude[exclude];
if(exclude == conn.userId){
continue connloop;
}
}
}
const browserEvent = {tags: intersectedTags, notification: eventPayload, exclusive};
conn.write(JSON.stringify(browserEvent));
}
})
} });

最佳答案

在闭包函数之外使用 continue 标签是不符合语法的,因为 continue 语句和标签 connloop 的作用域不同:

// continue statement in anonymous function
redisClient.SMEMBERS('connection/'+conn.id+'/subscriptions', (err, subscriptions)=>{

遵循代码在相同范围内应该没问题:

redisSubscriber.on("message", function(channel, event){

connloop:
for(let conn in connections){
// more codes ...
for(let exclude in event.address.exclude){
exclude = event.address.exclude[exclude];
if(exclude == conn.userId){
continue connloop;
}
}
}
});

关于javascript - 标记的 continue 语句; for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41293984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com