Novastorm
Novastorm

Reputation: 1458

Difference between using callbacks and calling methods

I'm very new to Javascript programming and was researching ways in dealing with asynchronous functions. I came across really helpful resource which lists this as an example:

var fs = require('fs')
var myNumber = undefined

function addOne(callback) {
    fs.readFile('number.txt', function doneReading(err, fileContents) {
        myNumber = parseInt(fileContents)
        myNumber++
        callback()
    })
}

function logMyNumber() {
    console.log(myNumber)
}

addOne(logMyNumber)

However could you achieve the same result doing this:

var fs = require('fs')
var myNumber = undefined

function addOne() {
    fs.readFile('number.txt', function doneReading(err, fileContents) {
        myNumber = parseInt(fileContents)
        myNumber++
        logMyNumber()
    })
}

function logMyNumber() {
    console.log(myNumber)
}

addOne()

And if you can, what would be the purpose/advantage of using callbacks?

For those interested the article came from here: https://github.com/maxogden/art-of-node#callbacks

Upvotes: 0

Views: 245

Answers (2)

Shailendra Sharma
Shailendra Sharma

Reputation: 6992

When we use call back it depend on situation in to make things dynamic or make sure that a code of piece run after one is complete.in your current code already describe callbacks

your first example clearly state how we define callbacks.

In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback, or it might happen at later time as in an asynchronous callback

var fs = require('fs')
var myNumber = undefined

you are using call back here which give you power to run different- different method after reading number.txt successfully

function addOne(callback) {
    fs.readFile('number.txt', function doneReading(err, fileContents) {
        myNumber = parseInt(fileContents)
        myNumber++
        callback()
    })
}

in your second example there is no callback you are calling logMyNumber() directly, what if we need to run another function something like

function logMyNumber() {
    console.log(myNumber)
}

function varifynumber() {
    console.log(myNumber)
}

function somthingelse() {
    console.log(myNumber)
}


addOne(logMyNumber)
addOne(somthingelse)
addOne(logMyNumber)

and the other best use of callbacks in JavaScript is handle asynchronous tasks, if you noticed inside your function you are using fs.readFile('number.txt',callback) which is a asynchronous method please have look below example

console.log('start');
 fs.readFile('number.txt', function doneReading(err, fileContents) {
          // until the file not read completely this section will not run 
          // this happend because of call back  
        console.log('Reading complete');
        })
console.log('End');

output :
start
End
Reading complete

i hope this will help you

Upvotes: 1

whirlwin
whirlwin

Reputation: 16521

It all depends on what you are trying to achieve. In the first example, the function addOne has no concept on what the callback parameter does, it just invokes it.

However, in the second case, the addOne function knows it will invoke logMyNumber, and therefore has a tighter coupling and concept of what exactly is going on.

The first example is often favorable in most cases, e.g. if you are splitting functions across multiple files, and don't want them to be tightly intertwined.

Upvotes: 0

Related Questions