The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Understand Callbacks in Node.js

Understand Callbacks in Node.js

7:03
In this lesson you will learn what callbacks are, how to use them and why we use them in our Node applications. We start with a simple javascript function and build a second function that uses it as a callback to understand the basic operation of a callback. From there, we create a few more functions that help illustrate how using callbacks can help us keep our code legible and organized as well as allowing us to use generic functions that can bring in extra functionality when needed via the callback.
Watch this lesson now
Avatar
egghead.io

In this lesson you will learn what callbacks are, how to use them and why we use them in our Node applications. We start with a simple javascript function and build a second function that uses it as a callback to understand the basic operation of a callback. From there, we create a few more functions that help illustrate how using callbacks can help us keep our code legible and organized as well as allowing us to use generic functions that can bring in extra functionality when needed via the callback.

Avatar
Tomasz

It is understandable why callbacks are introduced, but with native promise support (from version 4, I believe - we have version 7 now) callback's approach should be marginalized.

Avatar
Will

Yup, agreed.
This lesson was recorded when 0.12 was the latest and greatest. Callbacks are still a large part of the existing ecosystem, so it's good knowledge for those new to node.js. Stay tuned for some new lessons on Promises in node.js in the next couple of weeks!
Thanks for your feedback!

In reply to Tomasz
Avatar
Logan May

I'm new to node.js and a relatively new developer in general, so I have a fairly basic question. If welcomeMsg() prints the text and then returns undefined (which we see in our console,) and start calls welcomeMsg, then why doesn't calling start(welcomeMsg) print undefined twice? I thought it would - once for the call to start, which returns undefined, and once for the call to welcomeMsg, which does, as well.

Avatar
Will

Great question Logan.
When using the console, our interaction with Node is done with a REPL, which stands for Read - Evaluate - Print - Loop. The "Print" portion is the key here. In the REPL interface, it prints the output of whatever you give it and in cases where there is no output, the output is undefined. In the second example, welcomeMsg is called from the start function. The start function doesn't get a return value back, and isn't expecting one because it just executes the function. Had we done something different like

var foo = callback;
console.log(foo);

it would have printed undefined as you were expecting.
Another way of stating that might be to say that the REPL interface is tied to the start function. So, it Reads the start function, Evaluates it (or executes it), Prints whatever it got back, then Loops back to the cursor waiting for you to tell it what to do next. Everything that happened within the start function was done inside of Node, and only its return values were available to the REPL.
Hope that makes sense, if not- please let me know!

In reply to Logan May
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?