简体   繁体   English

使用node.js递归无限循环

[英]infinite loop recursively with node.js

I want to loop through some functions recursively so that after last function again it goes to first one and it goes forever. 我想递归地遍历一些函数,以便在最后一个函数之后再次到达第一个函数并永远消失。 Can anyone please help me out on that? 有人可以帮我吗? I also want to know if there is proper tutorial or something that will help me out to solve these problem in node js looping and flow controlling. 我还想知道是否有适当的教程或什么可以帮助我解决节点js循环和流控制中的这些问题。 I tried online tutorial though But not able to understand properly. 我虽然尝试了在线教程,但无法正确理解。

var util = require('util');
var async = require('async');
var SensorTag = require('./index');
var USE_READ = true;

SensorTag.discover(function(sensorTag) {
  console.log('discovered: ' + sensorTag);

  sensorTag.on('disconnect', function() {
    console.log('disconnected!');
    process.exit(0);
  });

  async.series([
  /*func 1*/
      function(callback) {
        console.log('connectAndSetUp');
        sensorTag.connectAndSetUp(callback);
      },
  /*func 2*/
      function(callback) {
        console.log('readDeviceName');
        sensorTag.readDeviceName(function(error, deviceName) {
          console.log('\tdevice name = ' + deviceName);
          callback();
        });
      },
   /*func 3*/
      function(callback) {
        console.log('readSystemId');
        sensorTag.readSystemId(function(error, systemId) {
          console.log('\tsystem id = ' + systemId);
          callback();
        });
      }
  })

})

When I executed it goes func 1 -> func 2 -> func 3 and then hangs. 执行时,功能1->功能2->功能3挂起。 I need to recursively loop these again and again. 我需要一次又一次地递归循环这些。

Edit code :I TRIED TO LOOP SOME FUNCTIONS, not all 编辑代码:我试图循环某些功能,但并非全部

var util = require('util');
var async = require('async');
var SensorTag = require('./index');
var USE_READ = true;

SensorTag.discover(function(sensorTag) {
  console.log('discovered: ' + sensorTag);

  sensorTag.on('disconnect', function() {
    console.log('disconnected!');
    process.exit(0);
  });

//function loop(){
  async.series([
  /*func 1*/
      function(callback) {
        console.log('connectAndSetUp');
        sensorTag.connectAndSetUp(callback);
      },
      function loop(callback){
        /*func 2*/
      function(callback) {
        console.log('readDeviceName');
        sensorTag.readDeviceName(function(error, deviceName) {
          console.log('\tdevice name = ' + deviceName);
          callback();
        });
      },
   /*func 3*/
      function(callback) {
        console.log('readSystemId');
        sensorTag.readSystemId(function(error, systemId) {
          console.log('\tsystem id = ' + systemId);
          callback();
        });
      },
      function(callback){
        loop()
        callback()
      }

      }
/*
      function(callback) {
        console.log('readDeviceName');
        sensorTag.readDeviceName(function(error, deviceName) {
          console.log('\tdevice name = ' + deviceName);
          callback();
        });
      },

      function(callback) {
        console.log('readSystemId');
        sensorTag.readSystemId(function(error, systemId) {
          console.log('\tsystem id = ' + systemId);
          callback();
        });
      },*/
      /*func 4*/
      loop();

  ]);
}



})

您可以使用async中forever函数 ,并在此调用async.series中

You could do 你可以做

function loop(){
    async.series([
        //  your func here
    /*func 4*/
    , function(callback){
        loop()
        callback()
    }
}

Edit: 编辑:

And then call it to start 然后调用它开始

loop();

Another edit: 另一个编辑:

Maybe there's no need for async at all 也许根本不需要异步

sensorTag.connectAndSetUp(readDevice);

function readDevice() {
   console.log('readDeviceName');
   sensorTag.readDeviceName(function(error, deviceName) {
       console.log('\tdevice name = ' + deviceName);
       readSystemId();
   });
}

function readSystemId() {
    console.log('readSystemId');
    sensorTag.readSystemId(function(error, systemId) {
        console.log('\tsystem id = ' + systemId);
        readDevice(); // start over again
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM