Skip to content

Instantly share code, notes, and snippets.

@khalid32
Created June 20, 2017 05:12
Show Gist options
  • Select an option

  • Save khalid32/0138303ee4191ec5fb2e8faf8eadcc73 to your computer and use it in GitHub Desktop.

Select an option

Save khalid32/0138303ee4191ec5fb2e8faf8eadcc73 to your computer and use it in GitHub Desktop.
An example which shows how to write a callback function and a callback-accepting function..
// Create a function that accepts another function as an argument
const callbackAcceptingFunction = (fn) => {
// Calls the function with any required arguments
return fn(1, 2, 3)
}
// Callback gets arguments from the above call
const callback = (arg1, arg2, arg3) => {
return arg1 + arg2 + arg3
}
// Passing a callback into a callback accepting function
const result = callbackAcceptingFunction(callback)
console.log(result) // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment