can any one give me a a simple example of nodeJs callbacks, I have already searched for the same on many websites but not able to understand it properly, Please give me a simple example.
getDbFiles(store, function(files){
getCdnFiles(store, function(files){
})
})
I want to do something like that...
Now open node or browser console and paste the above definitions.
Finally use it with this next line:
With Respect to the Node-Style Error Conventions
Costa asked what this would look like if we were to honor the node error callback conventions.
In this convention, the callback should expect to receive at least one argument, the first argument, as an error. Optionally we will have one or more additional arguments, depending on the context. In this case, the context is our above example.
Here I rewrite our example in this convention.
If we want to simulate an error case, we can define usingItNow like this
The final usage is exactly the same as in above:
The only difference in behavior would be contingent on which version of
usingItNow
you've defined: the one that feeds a "truthy value" (an Error object) to the callback for the first argument, or the one that feeds it null for the error argument.A callback function is simply a function you pass into another function so that function can call it at a later time. This is commonly seen in asynchronous APIs; the API call returns immediately because it is asynchronous, so you pass a function into it that the API can call when it's done performing its asynchronous task.
The simplest example I can think of in JavaScript is the
setTimeout()
function. It's a global function that accepts two arguments. The first argument is the callback function and the second argument is a delay in milliseconds. The function is designed to wait the appropriate amount of time, then invoke your callback function.You may have seen the above code before but just didn't realize the function you were passing in was called a callback function. We could rewrite the code above to make it more obvious.
Callbacks are used all over the place in Node because Node is built from the ground up to be asynchronous in everything that it does. Even when talking to the file system. That's why a ton of the internal Node APIs accept callback functions as arguments rather than returning data you can assign to a variable. Instead it will invoke your callback function, passing the data you wanted as an argument. For example, you could use Node's
fs
library to read a file. Thefs
module exposes two unique API functions:readFile
andreadFileSync
.The
readFile
function is asynchronous whilereadFileSync
is obviously not. You can see that they intend you to use the async calls whenever possible since they called themreadFile
andreadFileSync
instead ofreadFile
andreadFileAsync
. Here is an example of using both functions.Synchronous:
The code above blocks thread execution until all the contents of
test.txt
are read into memory and stored in the variabledata
. In node this is typically considered bad practice. There are times though when it's useful, such as when writing a quick little script to do something simple but tedious and you don't care much about saving every nanosecond of time that you can.Asynchronous (with callback):
First we create a callback function that accepts two arguments
err
anddata
. One problem with asynchronous functions is that it becomes more difficult to trap errors so a lot of callback-style APIs pass errors as the first argument to the callback function. It is best practice to check iferr
has a value before you do anything else. If so, stop execution of the callback and log the error.Synchronous calls have an advantage when there are thrown exceptions because you can simply catch them with a
try/catch
block.In asynchronous functions it doesn't work that way. The API call returns immediately so there is nothing to catch with the
try/catch
. Proper asynchronous APIs that use callbacks will always catch their own errors and then pass those errors into the callback where you can handle it as you see fit.In addition to callbacks though, there is another popular style of API that is commonly used called the promise. If you'd like to read about them then you can read the entire blog post I wrote based on this answer here.
we are creating a simple function as
Here is an example of copying text file with
fs.readFile
andfs.writeFile
:And that's an example of using
copyFile
function:Common node.js pattern suggests that the first argument of the callback function is an error. You should use this pattern because all control flow modules rely on it:
Try this example as simple as you can read, just copy save newfile.js do node newfile to run the application.
'fs' is a node module which helps you to read file. Callback function will make sure that your file named 'input.txt' is completely read before it gets executed. fs.stat() function is to get file information like file size, date created and date modified.