-1

I am trying to create a function echo with parameters callback and value that calls the callback function callback with value as its parameter.

Is someone able to briefly explain a callback function? Is it almost a nested function?

So far i have this:

function echo(value, callback) {
    return callback(value);
}

module.exports.echo = echo;
  • 1
    Functions in javascript are objects. They can be passed around like anything else. – Matt Ball Feb 11 '15 at 17:19
  • 1
    If you have it working, what exactly is the problem? – Bergi Feb 11 '15 at 17:20
  • maybe see also http://stackoverflow.com/questions/3523628/newbie-approach-what-is-a-javascript-callback-function – Bergi Feb 11 '15 at 17:23
  • its not working .. i meant to say i am working with cmd line js – DANIEL WHITE Feb 11 '15 at 17:23
  • A callback is simply a function that is provided to another function that should be called when the other function has completed. Usually the result of the operation is passed to the callback. Generally callbacks are provided to functions that execute in a different thread, which means you other code will continue to execute. When the code that was passed the callback finishes executing it will then invoke the callback to notify your JavaScript code. – Evan Shortiss Feb 11 '15 at 17:25
  • For example in Node.js you might do this: fileSystem.readFile('./file.txt', function (err, text) { if (err) { // Uh oh! } else { console.log('This printed second:', text); } }); console.log('This will print first!'); In that code the fileSystem.readFile code executes outside of the JavaScript thread and once complete it will invoke the callback you notify your JavaScript thread. Bare in mind this is a very high-level example and skips over the internals somewhat. – Evan Shortiss Feb 11 '15 at 17:25
  • works for me: http://jsfiddle.net/k0q6gh2v/ – Dr.Molle Feb 11 '15 at 17:27
  • 1
    What is not doing and what do you expect? If you call echo with a value and a callback, then it looks like the callback will be called with the value. That works. – Danny Staple Feb 12 '15 at 14:14

2 Answers2

1

A callback is just a variable that happens to hold something "callable". There is no further magic to it than that.

var myfunc = function() {
  alert("I was called");
}
var foo = myfunc;

foo();

The snippet above shows that a function is data or data can be a callable function. Just as data can be a string or printable as a string.

In JS, and a number of other high-level dynamic languages, the distinction between a function and other data is merely that it can be called. It can assigned, stored, made a member, returned and passed in as a parameter. In lower level languages, you are storing a pointer for the PC to jump to (function pointers).

Another way to see it is as a place holder - for a function that may be called when the other code reaches a particular state. I say may be - the other code may not reach that state as it is conditional - for example an error callback.

Danny Staple
  • 7,101
  • 4
  • 43
  • 56
  • A "variable that happens to hold something callable" is just a *function*. There are some more things that make a function argument a [callback](https://en.wikipedia.org/wiki/Callback_(computer_programming)) – Bergi Feb 11 '15 at 17:25
  • how would your example work with my code – DANIEL WHITE Feb 11 '15 at 17:25
  • @Bergi - The only thing that makes it a callback is the possibility that your code may be called later by some code which you passed it into - an event, a reactor/loop, a functional construct. There is nothing else special about it otherwise. The important thing here is the OP asked "make a function that calls a callback" - and all it really needs is a parameter that is callable, or contains an object that has a callable member for that to be the case. Any other "special treatment" is not needed. – Danny Staple Feb 12 '15 at 14:10
  • @DANIELWHITE your code shows you creating a function that takes a callback and value to call it with, which will call the callback when run. Do you mean a delegate - are you trying to create a bound function to be used in a callback? Bound to the value? – Danny Staple Feb 12 '15 at 14:23
0

A callback function is something you use when a function is completed and you want to return the result back to another function.

This is frequently used on asynchronous code:
Normally you would invoke a function, wait for it to complete and the functions returns a value back, however with asynchronous processes this will not work. So you need a callback. When the asynchronous function is invoked, the script will execute this in the background while the main thread continues. So you can't return a value in the normal way, since a synchronous function will halt the main thread until completed. When the asynchronous function is completed a callback is used to process the results.

Mouser
  • 13,132
  • 3
  • 28
  • 54
  • Not strictly "when completed". You could setup an object with a bunch of callbacks, and pass into a framework or something that calls different callbacks at different phases. Perhaps better phrased as "something you use when your code reaches a particular state, and you want to signal another function" - it provides other code the ability to add "hooks" into your code for additional handling of state. – Danny Staple Feb 12 '15 at 14:12