Nisha Patel
Problem
I am trying to terminate a long running loop in node.js using sockets.
I am emiting an event from react using socket.emit('eventName') and there is a socket.on('eventName') on server, which eventually suppose to update a boolean flag and terminate the running loop.
But when I emit an event, event emits but does not reflect at server end because loop is still running.
It terminates or takes input from socket after the loop ends. But I need to terminate the loop while it is executing.
[Update]
let booleanFlag = false //default value of flag
Below function will be called from socket.on('eventName')
This for loop which needs to be terminated, and will check the state of the variable
Solution
The JavaScript engine being monothread, it executes your synchronous loop in one go, without giving any chance to any other processing to occur, including the handling of your socket event (of course it will be handled at some point, but after your synchronous loop has ended).
To give a chance to your event to change your flag value, and the latter to be picked up inside the loop, you need to "pause" that loop regularly, typically by introducing an async operation, e.g.:
Answered by: >ghybs
Credit: >StackOverflow
Blog Links:
>How do I customize controller actions in Laravel Jetstream?
>Run Laravel Project to Active Local Server
>How to show encrypted user id in URL in Laravel?
>How to fix Laravel LiveWire listener?