Menu Close

How to export promises from one module to another module node.js?

Export promises from one module to another module NodeJS

In this article we will discuss how to export from one module to another module. 

Normally,  JavaScript is an asynchronous single-threaded programming language. Asynchronous means multiple processes are handled at the same time. Callback functions vary for the asynchronous nature of the JavaScript language. A callback is a function that is called when a task is completed, thus helps in preventing any kind of blocking and a callback function allows other code to run in the meantime, but the main problem with the callback function is the callback hell problem. The solution of callback hell is using the promises.

First Module: server.js

function check(number) {
  return new Promise((Resolve, reject) => {
    if (number % 2 == 0) {
      Resolve("The number is even")
    else {
      reject("The number is odd")
// Exporting check function
module.exports = {
  check: check

Second Module: index.js

// Importing function
const promise = require("./FirstModule.js")
// Promise handling
promise.check(7).then((msg) => {
}).catch((msg) => {

Keep both files in same folder and Run index.js file using the below command:

node index.js 


The number is odd 
Posted in NodeJs

You can also read...