0

I want to call an async function which result I am not interested in. Do I have to await it anyway if I don't want the function blocks the thread?

Is there a way to execute non-blocking code and not to wait for it?

Jero Lopez
  • 398
  • 1
  • 7
  • 18
  • 1
    You don't have to await an async function. You do if you care about the result. Also an async function can still be blocking. – evolutionxbox Jan 23 '23 at 19:28
  • If you don't want to wait for the async function, just take the `await` away. Nothing bad will happen (probably). – Fractalism Jan 23 '23 at 19:29
  • Note that if you don't `await` then any thrown errors won't be caught which may be undesirable (assuming you have a `try/catch`, of course) - you _could_ defer `awaiting` until the very end of your function (or simply `return` the `Promise`) to catch any errors. – Dai Jan 23 '23 at 19:30
  • 1
    "Is there a way to execute non-blocking code and not wait for it?" See Promise.then(function) – ControlAltDel Jan 23 '23 at 19:33
  • 1
    *"blocks the thread"*: there is no thread being blocked by the use (or not use) of `await`. If you have a problem where a thread is blocked, you have a problem that should be solved differently. `await` or `async` is not going to help with that. – trincot Jan 23 '23 at 20:19
  • @Fractalism "*Nothing bad will happen*" - unless the async function produces an error, which will probably crash the process. – Bergi Jan 23 '23 at 20:26
  • @Dai "*you could defer `await`ing until the very end of your function*" - [don't do that](https://stackoverflow.com/questions/46889290/waiting-for-more-than-one-concurrent-await-operation), [use `Promise.all` instead](https://stackoverflow.com/questions/45285129/any-difference-between-await-promise-all-and-multiple-await). – Bergi Jan 23 '23 at 20:27
  • @trincot for what do we use async then? – Jero Lopez Jan 24 '23 at 03:15
  • @evolutionxbox I am confused, I read you can use asyc to avoid your code blocks – Jero Lopez Jan 24 '23 at 03:26
  • *"for what do we use async then"*: for using APIs that have an asynchronous nature to it, like `setTimeout`, `fetch`,... These APIs use non-JavaScript technology to perform tasks while JavaScript can do other things in the mean time. – trincot Jan 24 '23 at 08:03
  • @trincot then my question is, if we remove the await in a call to an async function which uses such APIs, is the execution sync or async? As far as I understand, without the await it will execute a Promise, and promises are all async, however I read in several places that produces a sync call. I don't understand it.... – Jero Lopez Jan 24 '23 at 09:24
  • 1
    *"it will execute a Promise"*: promises are not functions -- they don't execute. *"promises are all async"*: promises are objects. async is a notion that relates to *jobs*. promises are not jobs, but they create jobs. When JS code calls a function, that function is **always** executed synchronously (whether `async function` or not). Asynchronous execution happens via a job that is placed in a job queue. `await` does that. – trincot Jan 24 '23 at 09:46
  • @trincot thank you for the answer but definitively I am very confused since I read opposite things in different sources... I will try to do my best to figure out how the whole thing works. Because I don't know yet what does really happen when an async function is called without await. – Jero Lopez Jan 24 '23 at 09:52
  • The function's execution does not depend on whether the caller has used `await` for it or not. In both scenarios the function will execute the same. The difference is that the caller's function will **return** when it uses `await` -- after the called function returns (yes, `await` results in a return!) `await` introduces a "savepoint" where the function will eventually *resume* execution, triggered by a job. – trincot Jan 24 '23 at 10:08
  • 1
    I'm not sure how much you know already, but this article is pretty good: [JavaScript Visualized: Promises & Async/Await](https://dev.to/lydiahallie/javascript-visualized-promises-async-await-5gke) – Darryl Noakes Jan 24 '23 at 12:53

0 Answers0