• Lmaydev@programming.dev
    link
    fedilink
    arrow-up
    40
    ·
    edit-2
    9 months ago

    A huge amount of time in apps is spent waiting for IO, database or web requests to complete.

    Async prevents locking a thread during this wait.

    If you’re handling a large amount of requests in a web server, for example, it allows other requests to progress while waiting for these operations.

    Threads are also expensive to start and manage.

    Also handling threads manually is a pain in the ass.

    • ShortFuse@lemmy.world
      link
      fedilink
      arrow-up
      4
      arrow-down
      1
      ·
      9 months ago

      Async prevents locking a thread during this wait.

      That’s a very common misconception. async is just a scheduling tool that runs at the end of event loop (microtask queue). It still runs on the main thread and you can still lock up your UI. You’d need Web Workers for actual multi-threading.

      • locuester@lemmy.zip
        link
        fedilink
        English
        arrow-up
        1
        ·
        9 months ago

        It can lock up a UI doing cpu bound work. Making a web request, no. Preventing the ui thread from waiting on native IO is what async was created for.

        • ShortFuse@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          9 months ago

          Preventing the ui thread from waiting on native IO is what async was created for.

          Citation needed. async just a wrapper for Promises. IO isn’t related, just commonly used with it.

          https://tc39.es/ecma262/multipage/control-abstraction-objects.html#sec-async-functions-abstract-operations-async-function-start

          NodeJS’s IO and fetch are just promises. (And NodeJS used to use callback(err, response) before adding promises.).

          • locuester@lemmy.zip
            link
            fedilink
            English
            arrow-up
            1
            ·
            9 months ago

            Yes I’m simplifying a LOT, but in the context of background web calls, that was what callbacks became so important for. XMLHttpRequest in IE 5 sparked the Ajax movement and adventures in nested callbacks.

            Prior to that, the browser had window.setTimeout and its callback for delays and animation and such - but that’s it.

            The main purpose of all this async callback stuff was originally, and arguably still is (in the browser), for allowing the ui event loop to run while network requests are made.

            NodeJS didn’t come into the picture for almost 10 years or so.

            • ShortFuse@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              edit-2
              9 months ago

              Yeah, that’s a big simplification and I get it. But the async syntax itself syntax “sugar” for Promises. It’s not like C# or Java/Android where it will spawn a thread. If you take a JSON of 1000 rows and attach a promise/await to each of them, you won’t hit the next event loop until they all run to completion.

              It’s a common misconception that asynchronous means “run in background”. It doesn’t. It means run at end of current call stack.

              Prior to that, the browser had window.setTimeout and its callback for delays and animation and such - but that’s it.

              And you STILL have to call setTimeout in your async executions or else you will stall your UI.

              Again async is NOT background. It’s run later. async wraps Promise which wraps queueMicrotask.

              Here is a stack overflow that explains it more in detail.

              • locuester@lemmy.zip
                link
                fedilink
                English
                arrow-up
                1
                ·
                9 months ago

                I’m well aware how async works in the single threaded js environment. All code blocks the main thread! Calling await on an async operation yields back.

                You’re right, async is commonly mixed up with multi-threaded. And in fact in many languages the two things work hand in hand. I’m very aware of how it works in JavaScript.

                We are agreeing. Don’t need more info.

    • Bene7rddso@feddit.de
      link
      fedilink
      arrow-up
      1
      ·
      9 months ago

      If you need to get multiple pieces of data for one request Async is great, but why would you work on different requests in the same thread? Why slow down one request because the other needs a bunch of computation?

      • Lmaydev@programming.dev
        link
        fedilink
        arrow-up
        3
        ·
        9 months ago

        You aren’t slowing down anything. If you didn’t use async that thread would be blocked.

        You’d need a thread per request even though they are sat doing nothing while waiting for responses.

        Instead when you hit an await that thread is freed for other work and when the wait is over the rest of the code is scheduled to run.