Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

socket.io remove object from array and ensure array update correctly if function ran twice the same time

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)
})

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

No special precautions are necessary.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading