Q:

NodeJS Tic Tac Toe STDIN Confliction

I am trying to make a game loop that runs this.turn() to continuously get user input. But when I run the code, the turn() functions all run at once and conflict with one another in STDIN. 
The program does not stop and wait for input before going to the next iteration and function call.
How can I make it stop and wait for the input before moving to the next iteration?
const readline = require('readline');

function TicTacToe () {

  this.board = [];
  this.playerTurn = '';
  this.state = true;

  this.intitializeBoard = () => {
    for ( let i = 0; i < 9; i++ ) {
      this.board.push(i);
    }
  }

  this.printBoard = () => {
    for (let i = 0; i < 7; i += 3 ) {
      console.log(this.board.slice(i, i + 3));
    }
  }

  this.determineFirstTurn = () => {
    let result = Math.floor(Math.random() * 2);
    if ( result == 0 ) {
      this.playerTurn = 'X';
    } else {
      this.playerTurn = 'O';
    }
  }

  this.prompt = () => {
    var rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout
    });

    rl.question('Enter the number of the square you wish to occupy\n', (input) => {
      if ( this.board[input] &&
            this.board[input] !== 'X' &&
            this.board[input] !== 'O' ) {

              this.board[input] = this.playerTurn;
              rl.close();
            } else {
              console.log("Incorrect input. Try again.");
              rl.close();
              this.prompt();
            }

    });
  }

  this.turn = () => {
    console.log("Player " + this.playerTurn + "'s Turn");

    this.printBoard();
    this.prompt();

    if ( this.playerTurn == 'X') {
      this.playerTurn = 'O';
    } else {
      this.playerTurn = 'X';
    }

  }

}

var game = new TicTacToe();

game.intitializeBoard();
game.determineFirstTurn();

game.board.forEach( () => {
  game.turn();
});

Terminal Output:
Player O's Turn
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
Enter the number of the square you wish to occupy
Player X's Turn
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
Enter the number of the square you wish to occupy
Player O's Turn
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
Enter the number of the square you wish to occupy
Player X's Turn
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
Enter the number of the square you wish to occupy
Player O's Turn
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
Enter the number of the square you wish to occupy
Player X's Turn
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
Enter the number of the square you wish to occupy
Player O's Turn
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
Enter the number of the square you wish to occupy
Player X's Turn
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
Enter the number of the square you wish to occupy
Player O's Turn
[ 0, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
Enter the number of the square you wish to occupy
222222222222222222

A:

This is typical for asynchronous code: you need to continue with dependent code when the callback is called.
This can also be solved with promises:
this.prompt = () => {
    var rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    // Promisify the question method:
    return new Promise( (resolve) => {
        rl.question('Enter the number of the square you wish to occupy\n', resolve)
    })
    .then((input) => {
        if ( this.board[input] !== undefined &&
                this.board[input] !== 'X' &&
                this.board[input] !== 'O' ) {
            this.board[input] = this.playerTurn;
            rl.close(rl);
        } else {
            console.log("Incorrect input. Try again.");
            return this.prompt(); // Return a new promise
        }
    });
}

Then in the turn method, you need to treat the this.prompt() call as a promise as well, and return a promise to the caller:
this.turn = () => {
    console.log("Player " + this.playerTurn + "'s Turn");

    this.printBoard();
    return this.prompt().then( _ => {
        if ( this.playerTurn == 'X') {
            this.playerTurn = 'O';
        } else {
            this.playerTurn = 'X';
        }
    });
}

Finally in the main code, you need to treat the return value from turn as a promise, and chain promise after promise with reduce instead of forEach:
game.board.reduce( prom => prom.then(game.turn), Promise.resolve() )
    .then( _ => {
        console.log('Game over');
        game.printBoard();
    });

Note I corrected a problem in this line of code, which will not work well for when input is 0
if ( this.board[input] && ...

Instead do:
if ( this.board[input] !== undefined && ...

