Putting function in Javascript function arugments?

There are three distinct concepts here used to great effect in JavaScript

Let’s say myfunction is the callee and the location of the call to myfunction is the caller

  1. Callback - a function passed as a parameter by the caller that can be called by the callee or by some other function known to the callee
  2. Async - function call whose complete processing does not end when callee returns - this means caller continues after callee returns but before the complete outcome of calling callee is known and processed
  3. Anonymous function - A function without a name also known as a lambda function in some languages - it can be merely convenient to use as a single-use function or something very powerful when used as a return value

These concepts are not new or unique to javascript - it’s their standard and pervasive use in javascript that make the language so special and exciting especially in the backend

In your code there is just one function call - to myfunction - the third parameter is an anonymous callback likely meant for async processing that occurs sometime later - when myfunction returns caller continues its processing

meanwhile when or as the full outcome of calling myfunction is known it is processed by the anonymous callback

the full outcome could be the handling of a UI event like a button click that cannot be predicted in advance - the caller in this case could be code to set up handlers for a calculation on a form - once the handlers are set up the caller is done - the callback could be a function that gathers various input fields and calculates a result shown on the web page after the button is clicked

it could be the response from a webserver that may take a while or timeout - the caller could be code that fires off multiple http requests to a webserver not waiting for one request to complete before firing the next - the callback could be a function that populates an HTML table with JSON data returned in the response

it could be records returned by a database query that are not available all at once - the caller could be code that prepares the query and sends it to the database server then exits - the callback could be a function that iterates over the records returned by the database using the data to update a chart in realtime

2 Likes