siddiq rehman
siddiq rehman

Reputation: 145

How to know what arguments have to be passed to the NodeJS call back functions?

I have got a code like

var dns = require('dns');

dns.resolve4('www.google.com', function (err, addresses) {
  if (err) throw err;

  console.log('addresses: ' + JSON.stringify(addresses));
});

How do we got the err, addresses arguments? what all other arguments can i pass? Thanks in advance.

Upvotes: 0

Views: 153

Answers (2)

abdulbari
abdulbari

Reputation: 6232

Callback as for standard it takes the first argument as error and second argument as result

Let's understand with simple example

/**
 * [executeRequest description]
 * @param  {[type]}   params   [params which need to pass]
 * @param  {[type]}   body     [body  which need to pass]
 * @param  {Function} callback [a callback function which finally executed after manipulation]
 * @return {[type]}            [callback with error or success]
 */
function executeRequest(params,body,callback){
//Execute some task here. it depends what this function does
 pass error in callback if get error like this
 callback(err);

 pass result if success
 callback(null,result);
}

Now call this function which needs to pass params, body and callback

executeRequest(params,body,function(err,result){
//Always the first argument of callback function is err and second argument is success 
})

Upvotes: 1

Daphoque
Daphoque

Reputation: 4678

It depends on function specification, ask to google :

npm dns resolve4

NPM doc

resolve4

Only two arguments, err and address

Upvotes: 0

Related Questions