J
J
jtag2018-05-31 07:04:55
JavaScript
jtag, 2018-05-31 07:04:55

How to make it so that after the event is processed, the next iteration in an infinite loop begins?

while(1) {
     сonfig.write(request, 'ascii');             //отправка запроса по TCP
     // если нет ответа то ждать
}
config.on('data', function(data){  //получение ответа
     if(data[18]==DOC_LIST) {
      console.log("Список карт: ", data)
     }
});

it is desirable to do without the global status flag, in the real code config.on is in a separate module and connected via request.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-05-31
@rockon404

Something like this:

checkTCP();

config.on('data', function(data) {
  if (data[18] === DOC_LIST) {
    console.log("Список карт: ", data);
  }
  checkTCP();
});

function checkTCP() {
  while(true) {
    сonfig.write(request, 'ascii');
  }
}

C
Coder321, 2018-05-31
@Coder321

Make a request with a promise and use async/await

async function infinityLoop() {
    for (; ;) {
        const res = await request();
        console.log(res)
    }
}

function request() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            return resolve(Date.now())
        }, 1000)
    })
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question