I have an electron application where I need to send a lot of data through UDP. This process is started through a click of a button in my React UI, and this example simplifies my problem. My React button:
<button
onClick={() => {
foo();
}}
>
Run foo
</button>;
Foo function:
export const foo = async () => {
while (true) {
console.log("hello");
}
};
While foo is asynchronous, it still freezes the React UI!
Since JavaScript is single-threaded, as soon as your infinite while loop async function is called it will block your thread. You can get around this by introducing an
await
inside your while loop, which will allow your program to return to executing other parts of your code.