Home > Back-end >  socket.io remove object from array and ensure array update correctly if function ran twice the same
socket.io remove object from array and ensure array update correctly if function ran twice the same

Time:01-23

I need to remove an object from an array. The issue is that there could be a case where this function is ran at the same time. In that case how can I ensure the array is updated correctly as the function my be called by two different users the same time

const rooms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

socket.on('disconnect', () => {
    var filtered = array.filter(e => {
        return e !== socket.id
    })

    console.log("room updated", filtered)
})

CodePudding user response:

JavaScript is a single-threaded language. 2 functions will never run in parallel.

No special precautions are necessary.

  • Related