Let's suppose we want to connect to our DB (whatever it is) through our NodeJS application, and that the code is as it follows:
let db = new Db()
let connection = db.connect()
db.findUserById('userID')
If we don't -somehow (for example, with Promises or a function*)- wait the connection to be finished (line 2), the findUserById will make the program crash because the db connection is not already done.
Well, let's now consider this other very simple (hope not too awkward) example:
let x = 3
let y = 5
let z = x * y * x * y * x * y // Just to simulate a time consuming operation
console.log(z)
In this second example, why the fourth line waits until the z variable has a value assigned and doesn't happen the same in the first example? Or, in other words: how can I know when a JS function is asynchronous?