JS中for循环嵌套switch时break引发的BUG!!!

  • Post author:
  • Post category:其他


JS中for循环嵌套switch时break引发的BUG!!!



问题描述

第一段for循环中必须使用

continue

作为

switch - case

的结束,如果使用

break

会导致

for

循环结束,为什么!!!难道是因为代码中包含了

Promise

?或者是

dispatcher



第二段for循环中没有使用

Promise



dispatcher

,它能正常使用

break

作为

switch - case

的结束。

正常情况下,

for

循环中嵌套

switch

时在

case

代码块中使用

break

是没有问题的,因为

break

是结束最近的一段代码块。



JS代码

  //函数名
  generateSlaveData(echoData) {
    const that = this;
    const { dispatch } = this.props;
    console.log('echoData', echoData);
    //引用参数
    let propsSlaveData = [];
    //存储parameters
    let parameters = [];
    const refs = echoData.refs;
    console.log('refs',refs)
    let nodeDeviceTreeSelect = that.props.baseInformationModel_base_cascade
      .getNodeDeviceByGidForTreeSelectRes
      ? that.props.baseInformationModel_base_cascade.getNodeDeviceByGidForTreeSelectRes.data
      : [];
    //好戏开始
    for (let i = 0; i < refs; i++) {
      switch (i) {
        case 0: {
          const param = Long.fromString(echoData.p0.toString(), true);
          const nid = param
            .shiftRight(16)
            .and(0xffff)
            .toString();
          const slaveId = param
            .shiftRight(32)
            .and(0xff)
            .toString();
          //转换成int类型,方便后面函数中switch
          const valuex = parseInt(
            param
              .shiftRight(40)
              .and(0xff)
              .toString()
          );
          const slaveData = [nid, slaveId];
          let refSlaveName;

          //发送请求
          dispatch({
            type: 'baseInformationModel_node/getDtypeById',
            payload: { nid: nid },
          }).then(function(res) {
            let rescode = '';
            const resData = that.props.baseInformationModel_node.getDtypeByIdRes;
            try {
              //获取结果状态码
              rescode = resData.statusCode;
            } catch (error) {}
            if (rescode == 200) {
              console.log('rescode', rescode);
              console.log('resData', resData.data);
              //生成Data数据
              nodeDeviceTreeSelect.forEach(node => {
                console.log('slaveData', slaveData);
                console.log('nnooddee', node);
                if (slaveData[0] == node.nid) {
                  refSlaveName = node.label;
                  node.children.forEach(device => {
                    if (slaveData[1] == device.slave_id) {
                      refSlaveName += ' / ' + device.label;
                    }
                  });
                }
              });
              console.log(refSlaveName);

              // let p = new Promise(function(resolve){
              //   getValueNameByDtypeValuex(resolve,resData.data,valuex)
              //   resolve('a')
              // })

              //返回promise
              let p = getValueNameByDtypeValuex(resData.data, valuex);
              //promise函数回调
              p.then(function(res) {
                console.log('pppppppppppppppppp', res);
                parameters.push({
                  index: i,
                  parameter: echoData.p0,
                  refSlaveId: slaveData,
                  refSlaveName: refSlaveName,
                  valuex: parseInt(valuex),
                  valuexName: res,
                });
              });
            } else {
              message.error('获取引用节点类型失败!');
              return false;
            }
          });
          console.log('0', nid, slaveId, valuex);
          //必须使用continue,如果使用break会导致for循环结束,为什么!!!难道是因为代码中包含了Promise?或者是dispatcher?
          continue;
        }
        case 1: {...}
        case 2: {...}
        case 3: {...}
      }
    }
    console.log('parameters', parameters);
    if (echoData) {
      //获取slave数量
      const slvs = echoData.slvs;
      console.log('slvs',slvs)
      //遍历slvs
      for (let i = 0; i < slvs; i++) {
        switch (i) {
          case 0: {
            propsSlaveData.push({
              index: i,
              deviceName: echoData.dname0,
              factor: echoData.f0,
              limitTag: echoData.lim0,
              lowLimits: echoData.low0,
              offset: echoData.o0,
              slaveId: echoData.slv0,
              upLimits: echoData.up0,
              parameters: parameters,
            });
            break;
          }
          case 1: {
            propsSlaveData.push({
              index: i,
              deviceName: echoData.dname1,
              factor: echoData.f1,
              limitTag: echoData.lim1,
              lowLimits: echoData.low1,
              offset: echoData.o1,
              slaveId: echoData.slv1,
              upLimits: echoData.up1,
            });
            break;
          }
          case 2: {
            propsSlaveData.push({
              index: i,
              deviceName: echoData.dname2,
              factor: echoData.f2,
              limitTag: echoData.lim2,
              lowLimits: echoData.low2,
              offset: echoData.o2,
              slaveId: echoData.slv2,
              upLimits: echoData.up2,
            });
            break;
          }
          case 3: {
            propsSlaveData.push({
              index: i,
              deviceName: echoData.dname3,
              factor: echoData.f3,
              limitTag: echoData.lim3,
              lowLimits: echoData.low3,
              offset: echoData.o3,
              slaveId: echoData.slv3,
              upLimits: echoData.up3,
            });
            break;
          }
        }
      }
    }
    console.log('propsSlaveData', propsSlaveData);
    return propsSlaveData;
  }



版权声明:本文为luckyman98原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。