-
-
Save auchomage/8e40e2165a00be528b0961a7b98bc1c4 to your computer and use it in GitHub Desktop.
module.exports = function(callback){ | |
function () { | |
// Contents of this anonymous function will serve as output | |
// from this file | |
console.log('** Output from decon_t2_calledFile.js ** '); | |
} | |
}; |
var someInput = require('./decon_t2_calledFile.js'); | |
// display contents of 'decon_t2_calliedFile.js' | |
someInput(function(callback){ | |
callback(); | |
}); // should display ('** Output from decon_t2_calledFile.js ** |
In line 1 of file decon_t2_calledFile.js, I declared a parameter that is meant to be referring the anonymous function declared on line 3. Is this right or wrong?
This is wrong. You are creating a parameter that's referring to the anonymous function passed into someInput on line 4 of decon_t2_callingFile.js. The anonymous function declared on line three of decon_t2_callingFile.js is not used or referenced anywhere in the program.
You want something like this:
// decon_t2_calledFile.js
module.exports = function(callback){
callback('Some string');
};
//decon_t2_callingFile.js
var someFunction = require('./decon_t2_calledFile.js');
// display contents of 'decon_t2_calliedFile.js'
someFunction(function(myString){
console.log(myString);
});
That's great, thank you for that.
I will study that and come back with questions.
One immediate question is this in the called file (ie decon_t2_calledFile.js).
- The anonymous function on line 3, is a function within a function - I thought this would be regarded as a callback.
- How could I refer to that function and pass it's output to the calling file so that the output serves as input for file (decon_t2_callingFile.ls)?
- Lets say in line 3 of the file decon_t2_calledFile.js. I had an anonymous function that performed a mathematical operation like return " 3 + 1".
Wouldn't I use something along the lines of :
function() {
return (3 + 1);
}
How can I define a function like that in decon_t2_callingFile.js and refer to it, to access it's output value?``
A function in a function is not a callback. A callback is generally defined as a function that runs after something else runs. In the example above the callback function would be the function defined on line 4 of decon_t2_callingFile.js. There is no need to define the function on line 3 decon_t2_calledFile.js. You can just call the callback param with whatever you like! It could be the result of 3 + 1.
A callback is generally defined as a function that runs after something else runs.
In this example we are working with, is that 'something else' equivalent to ' someFunction()'?
In the following example, we can see that the something else is a click event.
displayTodosButton.addEventListener('click', function(){ todoList.displayTodos(); });
Separate question:
Is it correct to say that
callback('some text');
Will result in 'some text' being generated as output?
You could say it's the result of the callback function. That would be appropriate.
A callback is generally defined as a function that runs after something else runs.
What does that statement mean in the context of :
//decon_t2_callingFile.js
var someFunction = require('./decon_t2_calledFile.js');
// display contents of 'decon_t2_calliedFile.js'
someFunction(function(myString){
console.log(myString);
});
What else is happening here? This will help me to identify it in future, as my previous definition of merely placing a function inside another function is wrong.
Thanks Andrew.
That "something else" would be anything asynchronous like fetching a webpage or making a database request. If you have synchronous code, callbacks are really adding any value because a simple return
statement could get the job done just fine.
I had created another example of using module.exports using node.js and accessing a string variable.
https://gist.github.com/auchomage/e066ac4d5660b81ebfecc195bc084846
This works
I then tried introducing a callback this decon_t2, and I can't get that to work, which is where I am now.