0

When you make a request to an API, the operation is usually asynchronous. I am wondering what actually makes these API requests asynchronous? Is it because we send the requests asynchronously(like using .then() function or async/await for promise) or the API server handles our requests asynchronously? I think it should be the former one? Besides, why some functions can be asynchronous without promise(such as setTimeout() and fs.readFile())?. Thanks!

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • "*When you make a request to an API, the operation is usually asynchronous.*" nomenclature: API is *anything* you can use in code. `parseInt()` is part of an API, as is `document.getElementById()`. There are *plenty* of synchronous APIs. What you refer to is specifically a *network request*. – VLAZ Sep 20 '22 at 11:41
  • Related but not quite: https://stackoverflow.com/questions/29883525/i-know-that-callback-function-runs-asynchronously-but-why/29885509. I think it's time to write a similar answer but include Promises and async/await – slebetman Sep 20 '22 at 12:39
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 20 '22 at 13:01
  • Yes. Thanks for correction. Is there any way to make a network request synchronously? – newlearner Sep 20 '22 at 13:06
  • @newlearner Not really*, since consider that the network request could take e.g. an hour (if you were to download a large file). (* the old XHR API still supports synchronous requests, but they aren't recommended) – AKX Sep 20 '22 at 13:08

1 Answers1

1
  1. The JavaScript runtime (e.g. Node.js or the browser) arranges for the request to be sent asynchronously, i.e. so your code gets notification of its progress or completion out of the usual order of code execution. What the server on its end does has nothing to do with it.
  2. Promises are the more modern way to deal with asynchronicity. setTimeout() uses "old-school" callback-style asynchronicity, and fs.readFile() uses Node.js's own brand of callback asynchronicity, where by convention the first argument of the callback is an error, if any occurred.
    • You can promisify callback-style asynchronous functions by hand by using new Promise(), or util.promisify in Node.js.
    • You can also callback-ify promise-style asynchronous functions, but in general you don't need to.
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Thank you for your answer. May I know how did people write callback-style asynchronous functions when there were no promises and callbackify function in the past? – newlearner Sep 20 '22 at 13:05
  • With... callbacks. `setTimeout(function() { alert("Hello!"); }, 1000);` is an async call to make your browser pop up an alert in 1000 milliseconds. – AKX Sep 20 '22 at 13:07
  • But if I write a function and put it as callback argument for another function, the another function won't become asynchronous right? Then how can these old callback-style functions be asynchronous? – newlearner Sep 20 '22 at 13:16
  • Because `setTimeout()` (or e.g. `fs.readFile`) is implemented by the JavaScript runtime in a way that allows _it_ to call the callback at a later time. – AKX Sep 20 '22 at 13:17