-1

I understand the concept of callbacks in Javascript. For example this sort of code makes complete sense to me -

function processArray(arr, callback) {
var resultArr = new Array(); 
for (var i = arr.length-1; i >= 0; i--)
    resultArr[i] = callback(arr[i]);
return resultArr;
}

var arr = [1, 2, 3, 4];
var arrReturned = processArray(arr, function(arg) {return arg * -1;});
// arrReturned would be [-1, -2, -3, -4]

I understand that callback is just a placeholder function which could be any function that we write later. However there are certain types of callbacks that I have no clue how to write. For example -

function processData (callback) {
fetchData(function (err, data) {
if (err) {
  console.log("An error has occured. Abort everything!");
  callback(err);
}
data += 1;
callback(data);
});
}

How is the callback to fetchData defined? How does the program know what is err and what is data?

3 Answers3

2

Normally in node.js the structure of a callback is basically like this function(err, result)

This means that the first argument is the error and the second argument is the data. when you have no error you simply pass a null value. This is a standard already.

The second code you posted you should change it like follows:

function processData (callback) {
    fetchData(function (err, data) {
        if (err) {
            console.log("An error has occured. Abort everything!");
            callback(err);
        }
        data += 1;
        callback(null, data);
    });
}

In the above case, if you did not want to increase the databy 1 you could simply call fetchData(callback).

DevAlien
  • 2,456
  • 15
  • 17
1

My two cents : In JavaScript everything is object and so does function. So functions can be passed as argument to other functions. This enables you to use callback as arguement and then using those callbacks in your api implementation to execute user's task after accomplishing API's task. Here if you would try to re-read your code then you will realize your misconception. Other users have also quoted the same thing here.

EDIT

function fetchdata(callback){
var err = "This is Error message";
var data = {samplekey : "samplevalue"};
callback(err, data); // here in the implementation of fetchdata
// we are making those data and feeding to the callback function.
}
function callback(err, data){
// Implementation of call back
} 

Here for making readable purpose I have put callback function separate. You can pass it simply as well.

binariedMe
  • 4,309
  • 1
  • 18
  • 34
  • I understand that callbacks can be used as arguments. But in case of the second example, where is err and data defined? BTW i got this example here 0 [link](https://docs.nodejitsu.com/articles/getting-started/control-flow/what-are-callbacks) – PositivelyDoped Sep 08 '15 at 14:30
  • err and data will be most probably fed by fetchdata function. An example I am attaching in the edit section of my answer. – binariedMe Sep 08 '15 at 17:44
0

I think you do not understand JavaScript completely. I got this example from here

function doSomething(callback) {
    // ...

    // Call the callback
    callback('stuff', 'goes', 'here');
}

function foo(a, b, c) {
    // I'm the callback
    alert(a + " " + b + " " + c);
}

doSomething(foo);
Community
  • 1
  • 1
mryuso92
  • 267
  • 4
  • 21
  • I understand this. And I understand the first code in my question. If I am correct, a callback is just a function that you want your main function to execute. So you pass the callback as an argument and you have the freedom to define the callback outside the scope of the main function. This is what I see in your example as well. My confusion is that sometimes the callbacks are not defined, but just executed like in the second example. This is weird because the arguments of the callback no longer act as placeholders. For instance in my question, what is data? Where is that defined? – PositivelyDoped Sep 08 '15 at 14:28