Just curious what's a good way to comment what parameters will be passed to the callback function.
Suppose we have the following code and incomplete comments
/**
* an utterly useless function
*
* @param {String} an useless string
* @param {Boolean} an useless boolean
* @param {Function} ???
*/
function Useless (str, bool, callback) {
callback(str, bool);
}
What's a good way to say callback will be called with str and bool are parameters?
Callbacks make sure that a function is not going to run before a task is completed but will run right after the task has completed. It helps us develop asynchronous JavaScript code and keeps us safe from problems and errors.
A JavaScript callback is a function which is to be executed after another function has finished execution. A more formal definition would be - Any function that is passed as an argument to another function so that it can be executed in that other function is called as a callback function.
A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.
Usually you will just write an invocation of the function with speaking names:
/*
* @param {String} input: the text
* @param {Function} callback(output, hasChanged): called after calculation
*/
Or, if the parameters need explanation, you can use a multiline description:
/*
* @param {String} input: the text
* @param {Function} callback(result, change)
* the function that is called after calculation
* result {String}: the output of the computation
* change {Boolean}: whether a change has occurred
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With