Skip to content

Instantly share code, notes, and snippets.

@auchomage
Created July 10, 2016 22:26
Show Gist options
  • Save auchomage/8e40e2165a00be528b0961a7b98bc1c4 to your computer and use it in GitHub Desktop.
Save auchomage/8e40e2165a00be528b0961a7b98bc1c4 to your computer and use it in GitHub Desktop.
Node.JS callbacks - question 1: I have 2 files (1) decon_t2_calledFile.js and (2) decon_t2_callingFile.js. My aim is to take the string output of file 1 (decon_t2_calledFile.js) and access through file 2 (decon_t2_callingFile.js))
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 **
@auchomage
Copy link
Author

auchomage commented Jul 10, 2016

Whenever I try to run this from the command prompt using 'node decon_t2_callingFile.js',
I get the following error message:
/home/ocj/node-course/async2/decon_t2_calledFile.js:3
function () {
^
SyntaxError: Unexpected token (...

What am I doing wrong?

In file ' decon_t2_calledFile.js'
I have:

  1. defined a parameter as a 'callback' (this is meant to serve as an output channel for the output from the 'callback'
  2. created an anonymous function to serve as the 'callback'

In file ' decon_t2_callingFile.js '
I have

  1. created a 'callback' function that is acting as an argument to someInput().
  2. I attempted to run the 'callback' by calling 'callback()'

What am I doing wrong?
Thanks

@andrewjmead
Copy link

On line 4 of the calling file an anonymous function is passed as the argument to the someInput function. That function is available is the called file via the callback argument. The only problem is it's never used.

In the called callback file you need to pass that function you define on line 3 into the callback function. Then everything should work!

@auchomage
Copy link
Author

I'm missing something here which is important.
In the called file ( decon_t2_calledFile.js),I thought the function defined on line 3 is the 'callback' function. (It is a function within a function). I specifically defined 'callback' as a parameter.
(Because I think this is the channel which the output from 'callback' will use to transfer the data ie string ('** Output from decon_t2_calledFile.js ** ') will use to go to the calling file ie ( decon_t2_callingFile.js).
Is that right or wrong?

In the calling file( decon_t2_callingFile.js), I pass the callback as argument to an anonymous function. The anonymous function acts as an input to someInput().
The contents of the anonymous function a function call to 'callback()'. This is referring to line 3 of the file ( decon_t2_calledFile.js). I thought was the callback function I was referring to and by simply stating 'callback()', I am calling for it to be executed.
So when you say, I never call it. I'm confused.

If you don't call it that way, how do you call it. (I'm specifically trying to find out how to access the contents of the function on line 3 of the called file).
Thanks for your patience.

Jerome

@andrewjmead
Copy link

In the called file ( decon_t2_calledFile.js),I thought the function defined on line 3 is the 'callback' function.

The function defined on line 3 is not used at all. It's not returned, it's not passed to a function, and it's not given a variable name. The example is confusing because both files are expecting a callback to be passed into them.

The contents of the anonymous function a function call to 'callback()'. This is referring to line 3 of the file ( decon_t2_calledFile.js)

Nothing is referring to that function because it's never used as stated above. Also, the function defined on line 1 of the called file gets passed a callback function but that callback function is never getting called.

Do you see why that function on line 3 is never getting called/used?

@auchomage
Copy link
Author

auchomage commented Jul 12, 2016

Do you see why that function on line 3 is never getting called/used?
The answer is no, don't.

The sole aim was to create a call back in file ( decon_t2_calledFile.js), then access the contents from file (decon_t2_callingFile.js).

I will have to drill down to basics here. (I'm sorry, I really am missing something very elementary here).

File: decon_t2_calledFile.js
Aim: Create a file that has one function to output a string and export that string to another file (in this case the file is ' decon_t2_callingFile.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?

File decon_t2_callingFile.js
On line 1, a variable 'someInput' was declared to capture the output of file 'decon_t2_calledFile.js' - (I think that is correct)

The next step is where I think I'm getting confused. I want to be able to reference the callback output from file
What code do I write at this point that will allow me access the output of the file 'decon_t2_calledFile.js'?

I tried thinking it through and failed, I tried various combinations and failed. Hence this request.
I tried "someInput(callback)", that failed, and various other things like that.

This is what I need to know and understand.

@auchomage
Copy link
Author

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.

@andrewjmead
Copy link

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);
});

@auchomage
Copy link
Author

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).

  1. The anonymous function on line 3, is a function within a function - I thought this would be regarded as a callback.
  2. 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)?
  3. 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?``

@andrewjmead
Copy link

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.

@auchomage
Copy link
Author

auchomage commented Jul 18, 2016

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?

@andrewjmead
Copy link

You could say it's the result of the callback function. That would be appropriate.

@auchomage
Copy link
Author

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.

@andrewjmead
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment