The function inside getData
is a callback being passed to setTimeout
, which is one way to schedule a call to a function to happen in the future. In this case, it's asking for that callback to happen roughly 100ms later. getData
returns before that happens.
The setTimeout
callback is a closure¹ over the context where it's created, which means it has access to cb
even after getData
has returned. So when the browser's timer calls the callback, the callback can call cb
. The call to cb
uses an object literal to create an object to pass to cb
.
In the call to getData
, the author is passing a function as cb
that logs the data it receives.
So:
getData
is called, passing in a function that will log the argument it gets.
getData
calls setTimeout
to schedule a callback in about 100ms, passing in another function for the timer to call.
getData
returns.
About 100ms later, the browser's timer subsystem triggers a call to the callback passed to setTimeout
.
That callback creates an object and calls cb
, passing the object to it.
That callback (the one passed to getData
) logs the data
object it receives.
¹ "closure" — see: SO, my anemic blog