-1

So I tried doing something like this

async function() {

    while (x) {

    }
    // code
}

The reason I want to do this is because I need something to be done only when X becomes false, and I want it to be in another thread so my website is still usable, and according to the naming and what I read about it, async function should be async... It's only natural right? But instead, my application is completely frozen when the while loop is running. Can someone please explain why this happens and if there's a way around it?

Thank you.

3
  • You could check the value periodically like with setTimeout in browser. Where does your code run? Commented Feb 10, 2019 at 10:58
  • 1
    @FelixKling that is misleading, the async function will be executed right at the point when it is called, like any other function (until the first await appears). Its result is scheduled to resolve after the current tick ends. Commented Feb 10, 2019 at 11:00
  • @t.niese: You are right... it's late. Will remove my comment. Commented Feb 10, 2019 at 11:01

2 Answers 2

4

async does not mean multithreading, it just means that you can use the await keyword it it and that the value returned by that function is guaranteed to be a Promise, that its result will always be resolve/rejected in an asynchronous way.

So if the code within the async function does not utilizes any other asynchronous code then it would block the rest of the code the same way as a regular function would do.

If you use await within your while loop, then other code that is waiting could interleave at that point.

async function() {

    while (x) {
        // ...
        await someFunction() // at this `await` other code could be executed
        // ...
    }
    // code
}

So a await in the async function does two things, if the right side of the await is a Promise, then it will wait until it is resolve (or rejected) and it will allow other code that is scheduled to be executed to interleave.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry but the async directive seems so useless to me now. I see no reason to use it. I can just setTimeout(func, 0); and put a while loop inside of the func that I pass to it and it will not freeze the thread and won't interrupt any code. It's really annoying how they name the directive async, and it does a completely different thing. Like who the hell decided it should be this way
@FightRay I can just setTimeout(func, 0); and put a while loop inside of the func that I pass to it and it will not freeze the thread thats not true, as sonn as the as the func will be called the while loop will block your code, and no other code will execute until the func is finished, or if the code actively allows the execution to another waiting code (using await or yield, or calling an async API). But there is no way to do multi threading in javascript, there is multiprocessing using workers, but no multithreading.
@FightRay And it does what the name says, you only expect from the name something different. It's how asynchronicity was always defined in JavaScript. And the async/await syntax is extremely useful, you can now write asynchronous code that looks almost like synchronous one, and you can use loops (for, while) instead of ugly constructs like Promise.all(list.map( ... )), because await can interrupt the execution of the loop (for ,while, ...) allowing other code to run, so your loop won't be blocking. But it does not make our code magically non blocking.
0

I just wanted to share how I fixed my issue. It might not be the cleanest solution or the right solution, but it did just what I wanted with no performance issues.

All I wanted is to make a while loop that doesn't interrupt my code and at least seems like it runs asynchronously... and the async keyword wasn't what I was hoping it was, and doesn't do anything close to it. Instead, I tried this solution and it worked perfectly.

setTimeout(function() {
    while (true) {
        if (x) {
            break;
        }
    }
    // code
}, 0);

It seems that when you type 0 for the delay in a setTimeout, it executes the function you pass to it only after all of the pending code has executed. And so, it makes it act like it's kind of an async function. It accomplished what I wanted and works seamlessly, so that's what's improtant. If there's a better simple solution, please inform me.

Thank you.

1 Comment

setTimeout does not create multithreading either. It is basicly the same as the code of your question, except that now the code is scheduled after the current tick ends. But it still does not create multithreading, and does not allow other code to execute while you are in the while(true) loop. If x is false at the time when the event loop calls that function, then the while (true) loop will run forever, because x could never change (except of code that is executed from within the loop).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.