JS, like any interpreted language, is run line-by-line. In your example, at the parseServiceCodes
callsite, execution goes from line 2 (the callsite) to line 6 (the function body).
Async behavior is introduced for time consuming operations, like HTTP requests. In this case, the expensive function either takes a callback parameter (code to run once the expensive operation is over) or returns a Promise
which "resolves" with the computed data when ready.
To illustrate the first case, let's say we have a function getFromServer
which needs a URI and a callback:
function getFromServer(uri, callback){ // ...
It doesn't matter exactly what happens under the hood, just that execution is given back to the main program after invocation. This means the program isn't blocked for the duration of this expensive operation. When getFromServer
is done, it will come back and call callback
with some data.
To learn more about the second case, Promises, I encourage you to read the documentation I linked to earlier in this response.