Skip to content

Instantly share code, notes, and snippets.

@Sidsector9
Last active June 5, 2020 05:01
Show Gist options
  • Select an option

  • Save Sidsector9/78b10e94bb34accdda6e472dd884aacc to your computer and use it in GitHub Desktop.

Select an option

Save Sidsector9/78b10e94bb34accdda6e472dd884aacc to your computer and use it in GitHub Desktop.
/**
* This function provides 2 callback functions - before and after.
* `runBefore` is called beforer executing the thousand lines of code and
* `runAfter` is called after executing the thousand lines of code.
*/
function functionThatDoesSomething( name, runBefore, runAfter ) {
/**
* It is important to check the type else it will throw an error.
*/
if ( 'function' === typeof runBefore ) {
runBefore( name );
}
/**
* Some thousands line of code
* .
* .
* .
* .
* .
*/
/**
* It is important to check the type else it will throw an error.
*/
if ( 'function' === typeof runAfter ) {
runAfter( name );
}
}
/**
* Now you can provide your own callback functions, they can be named or anonymous.
*/
functionThatDoesSomething( 'Kaavya', greet, informEnd );
/**
* This will be called in place of `runBefore` at line 13.
*
* The value of `someVar` will be `name` because that is what
* is passed at line 13.
*/
function greet( someVar ) {
console.log( 'Welcome ' + someVar + '!' );
}
/**
* This will be called in place of `runAfter` at line 29.
*
* The value of `someVar` will be `name` because that is what
* is passed at line 29.
*/
function informEnd( somevar ) {
console.log( 'The function finished executing, what next ' + somevar + '?' );
}
/**
* This file starts executing at line 36.
*
* 36: `functionThatDoesSomething` is called with 3 arguments: 'Kaavya', greet function, informEnd function.
* 12: Checks if `runBefore` is a function, which is true because we passed `greet` function to it. It calls runBefore and passes it `name`
* 44: console.logs at line 45
* 16-27: Executes these lines
* 28: Checks if `runAfter` is a function, which is true because we passed `informEnd` function to it. It calls runAfter and passes it `name`
* 54: console.logs at line 55
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment