chxru
chxru

Reputation: 581

Create callback in node js module.exports

How to create a callback function inside the module.exports parameter. I'm trying to do a similar thing as below and I want to know how to implement callback function.

module.js

module.exports = (a, b, callback) => {
  let sum = a+b
  let error = null
  //callback
}

app.js

const add = require(./module.js)

  add(1,2, (err, result) => {

  }

Upvotes: 2

Views: 8734

Answers (3)

Shuki
Shuki

Reputation: 310

within your module.exports you need to "invoke" the call back function. like so

callback(error, sum)

this will return the control back to the app.js add() function. You implement your callback function here. i.e what you want to do with the result you received.

Here is what your code will look like:-

module.js

    module.exports = (a, b, callback) => {
      let sum = a+b
      let error = null
      callback(error, sum) // invoke the callback function
    }

app.js

    const add = require("./module")

    add(1,2, (err, result) => {
      if(err) { // Best practice to handle your errors
          console.log(err)
      } else { // Implement the logic, what you want to do once you recieve the response back 
        console.log(result) 
      }
    })

Upvotes: 8

Bharathvaj Ganesan
Bharathvaj Ganesan

Reputation: 3194

add.js

module.exports = (a, b, callback) => {
   if (typeof a !== 'number' || typeof b !== 'number') {
      return callback(new Error('Invalid argument passed'), null);
   }
   let sum = a + b;
    callback(null, sum);
};

app.js

const add = require('./add');

add(1, 2, (err, result) => {
  if (err) {
     console.log(err);
  }
  console.log(result);
});

Here we are passing error as the first parameter and actual sum as the second parameter to the callback function. Say if we pass a string instead of the number then the first parameter will have the error object and result will be null.

Cheers.

Upvotes: 1

Prasanna
Prasanna

Reputation: 4636

You have used sum for your function; but I will be using divide, because that way I can show you the error thing of callback.

your export will look like this

module.exports = {
  divide: (a,b,cb) => {
    if (b === 0) {
      cb('divide by zero', null);
    } else {
      cb(null, a/b);
    }
  }
}

and the import like this

var func = require('./testExport').divide;
func(1,2,(err,res) => {
  console.log(err,res);
});
func(1,0,(err,res) => {
  console.log(err,res);
})

Call backs are simply the function that you send in from the place you are calling the functions. In both function calls (in imported place) you see we are sending in a function as a callback that takes in two arguments.

In the exported place we call that same function with the first parameter as an error and the second as res.

If you want to import your function without require().func, you will have to export the function in default.

module.exports = (a,b,cb) => {
  if (b === 0) {
    cb('divide by zero', null);
  } else {
    cb(null, a/b);
  }
}

and import it as

var defaultFunc = require('./testExport')

Upvotes: 2

Related Questions