标记继续声明; for循环

时间:2016-12-23 01:21:30

标签: javascript node.js ecmascript-6

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));
      }
  })
} });

2 个答案:

答案 0 :(得分:2)

continue跨功能边界无效。你将continue放在一个函数中,试图跳转到函数外的位置。

由于continue很可能是异步操作,因此在redisClient.SMEMBERS之间也没有意义,因此循环在调用回调时已经终止。

答案 1 :(得分:1)

使用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;
            }
        }
    }
});