0

I have two specific cases where I don't quite understand what's going on.

Case 1

// env = node.js
async function myReadFile1(...args) {
    const {readFileSync} = require("fs")
    return readFileSync(...args)
}

async function myReadFile2(...args) {
    const {readFile} = require("fs/promises")
    return await readFile(...args)
}

Question: are these two functions equivalent? If not, what is readFile from fs/promises doing differently to make it non blocking?

Case 2

// env = browser

<script>
    function run() {
        let x = 0
        while (true) {
             x++   
        }
    }
    run()
</script>

<script>
    async function asyncRun() {
        let x = 0
        while (true) {
             x++   
        }
    }
    asyncRun()
</script>

When I insert either one of these scripts into my index.html and open it with a browser, the page is non-responsive. With the first script, I understand why. There's a blocking infinite loop. But with the second one, I understand it less. I naively thought that since it is an async function, it'd be non-blocking, with its own space in the event loop. Yet, the page is still unresponsive.

So clearly there's something about how async works that I'm missing and that I couldn't find any explaining resources for. Could someone point me in the right direction?

  • 2
    Async functions only yield control when they reach an await, all you've done by adding async there is return a promise, not make anything actually asynchronous. – jonrsharpe Sep 15 '22 at 07:53
  • 1
    to be honest, you'd never write `myReadFile2` like that ... `return await somePromise` for a start is just `return somePromise` - and since now that function has no `await` ... you wouldn't even make it `async` – Jaromanda X Sep 15 '22 at 07:53
  • In `case 2` there is no await so the function will still block execution – mousetail Sep 15 '22 at 07:54
  • [One question per post please](https://stackoverflow.com/help/on-topic). – Ivar Sep 15 '22 at 07:55
  • Thanks all for the comments! I see now that there are other questions that deal with this on here, and that, as @jonrsharpe pointed out immediately, control is relinquished from the async function when there is an await - I didn't know that. Thank you all! – Winkler Áron Sep 15 '22 at 08:00
  • Even with an `await` it does not really make it asynchronous. It will just execute everything else and then synchronously execute the body of the async function at the end. What makes `readFile` asynchronous are asynchronous file API provided by the OS available in C/C++. To create truly asynchronous functions in javascript you need to extend javascript using C/C++ (eg. write a C++ module for node.js or compile your own modified web browser) – slebetman Sep 15 '22 at 08:04
  • ... or use webworkers/worker_threads – slebetman Sep 15 '22 at 08:04
  • @slebetman That depends on how you define _asynchronous_. And whether you would say `setTimeout` and/or `setImmediate` is part of the JavaScript or not. But if there aren't any asynchronous API provided by the JavaScript environment then `await,` and `async` are useless. But with `await` and `async` you can do cooperative multitasking. So you can split long-running tasks into smaller chunks, and utilize `await` to "yield control" so that other code can run in between. `async` and `await` for sure won't change anything about `asyncRun` being blocking. – t.niese Sep 15 '22 at 11:07
  • ... But you could utilize `await` the `while` loop of `asyncRun` to make it "cooperative". – t.niese Sep 15 '22 at 11:09

0 Answers0