Created
June 20, 2017 05:12
-
-
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..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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