Socket.io event handler bug

Take a note with onPull event handler. The console.log() is called before textHistory.push(), but the console displays textHistory array with one item inside it, whereas it was an empty array before.

// server.js
import fs from 'node:fs'
import http from 'node:http'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import chokidar from 'chokidar'
import express from 'express'
import { Server } from 'socket.io'
import { createServer as createViteServer } from 'vite'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

let version = 0

async function createServer() {
  const app = express()
  const server = http.createServer(app)
  const io = new Server(server)
  const watcher = chokidar.watch('./note.txt', {
    ignoreInitial: true
  })

  const vite = await createViteServer({
    server: { middlewareMode: true },
    appType: 'custom'
  })

  app.use(vite.middlewares)

  app.get('/', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'index.html'))
  })

  io.on('connection', (socket) => {
    socket.emit('pull', {
      version,
      data: fs.readFileSync('./note.txt', 'utf8')
    })

    socket.on('push', (data) => {
      fs.writeFileSync('./note.txt', data)
    })

    watcher.on('change', () => {
      version++
      socket.emit('pull', {
        version,
        data: fs.readFileSync('./note.txt', 'utf8')
      })
    })
  })

  server.listen(3000)
}

createServer()
// index.html
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta content="width=device-width" name="viewport">
  </head>
  <body>
    <textarea
      autocapitalize="off"
      class="text-editor text-editor--type-push"
      cols="20"
      id="pushTextEditor"
      rows="10"
    ></textarea>
    <textarea
      autocapitalize="off"
      class="text-editor text-editor--type-pull"
      cols="20"
      disabled
      id="pullTextEditor"
      rows="10"
    ></textarea>
    <div class="push-indicator" id="push-indicator"></div>
    <button disabled id="push">Push</button>
    <button disabled id="pull">Pull</button>
    <p id="length"></p>
    <script src="/main.js" type="module"></script>
  </body>
</html>
// main.js
import eruda from 'eruda'
import { io } from 'socket.io-client'
import './style.css'

eruda.init()

const socket = io()

const pushTextEditorElement = document.getElementById('pushTextEditor')
const pullTextEditorElement = document.getElementById('pullTextEditor')
const pushIndicatorElement = document.getElementById('push-indicator')
const pushElement = document.getElementById('push')
const pullElement = document.getElementById('pull')

const textHistory = []
let initialized = false
let isSync
let pushing = false
let pushTimeout
const pushDelay = 1000

function handleInput(event) {
  clearTimeout(pushTimeout)
  pushTimeout = setTimeout(() => push(event.target.value), pushDelay)
}

function push(data) {
  pushing = true
  pushIndicatorElement.classList.add('push-indicator--active')
  textHistory.push({
    version: textHistory[textHistory.length - 1].version + 1,
    data
  })
  socket.emit('push', data)
}

socket.on('connect', onConnect)
socket.on('pull', onPull)
socket.on('disconnect', onDisconnect)

function onConnect() {
  pushTextEditorElement.addEventListener('input', handleInput)
  console.log('connected')
}

function onPull(pullObject) {
  /* bug here */
  console.log(textHistory)

  if (!initialized) {
    initialized = true
    isSync = true
    textHistory.push(pullObject)
    pushTextEditorElement.value = pullObject.data
    pullTextEditorElement.value = pullObject.data
  }
}

function onDisconnect() {
  pushTextEditorElement.removeEventListener('input', handleInput)
}

Here is the project code socket.io-playground

However, if I remove the console.log() and use other display method like writing into the dom with document.getElementById('...').textContent = textHistory.length, it displays 0!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật