Home > Mobile >  how to stop infinite loop when using callback inside while-loop in js
how to stop infinite loop when using callback inside while-loop in js

Time:11-27

so i'm creating a game like Connect 4 which ask an input from a user but the problem i'm facing is that i used callback(readline.question) function inside a while loop whenever i start the code it start infinite loop without asking a question from a user. how i can pause it for a while until user answer? I've to solve this without using async/await.

 function fetchColumn(player, callback) {
   io.question(`Player ${player}, which Column? `, line => {
        console.log(`You requested "${line}"`);
        chosen_column = line;
        callback(); 
    });
}
let connect4 = new Connect4();
connect4.makeBoard(numRows, numCols, winLength);
while (game_over == 0) {
    connect4.printBoard();
    fetchColumn(current_player,()=>{
    
        console.log(`you entered ${chosen_column}`);
        
        if (chosen_column != 'Q' && chosen_column != 'q') {
            move_status = connect4.place_piece(chosen_column, current_player);
            x_in_a_row_status = connect4.x_in_a_row(current_player);
            
            if (move_status == 0) {
// Further code-------

This is what i'm getting in terminal.

Player 1, which Column? 
A B C D E F G 
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . 
Player 1, which Column? 
A B C D E F G
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

----------Keep repeating----------

CodePudding user response:

If you want to call an asynchronous function in a loop, you can either use while in combination with await:

function fetchColumnAsync(player) {
  return new Promise(function(resolve, reject) {
    io.question(`Player ${player}, which Column? `, line => {
      console.log(`You requested "${line}"`);
      chosen_column = line;
      resolve(); 
    });
  });
}
let connect4 = new Connect4();
connect4.makeBoard(numRows, numCols, winLength);
while (game_over == 0) {
  connect4.printBoard();
  await fetchColumnAsync(current_player);
  console.log(`you entered ${chosen_column}`);
  ...
}

or recursion in combination with a callback function:

function loop() {
  if (game_over == 0) {
    connect4.printBoard();
    fetchColumn(current_player, () => {
      console.log(`you entered ${chosen_column}`);
      ...
      loop();
    });
  }
}
let connect4 = new Connect4();
connect4.makeBoard(numRows, numCols, winLength);
loop();

But you cannot combine while with a callback, because the second iteration of the while loop starts synchronously, before the callback function can be invoked asynchronously. In other words, it gives an infinite loop.

  • Related