Open In App
Related Articles

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

Improve Article
Save Article
Like Article

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 in this article we will discuss how to export from one module to another module. 

Project Structure: It will look like this.


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


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

Run SecondModule.js file using the below command:

node SecondModule.js


The number is even
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 26 May, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials