Open In App

How to return an error if response not received in 1 minute in Node.js ?

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Node.js, sometimes due to some errors, a response takes more time than usual and in this case, instead of waiting for an uncertain period, we can return an error or some message if the response is not received in 1 minute. 

Problem: Suppose there is a function ‘helloGeeks’ which in response returns a promise, then the returns value is logged by using the keyword ‘then’. The function ‘helloGeeks’ take too time to give a response and this process stop the flow of execution.

Javascript




let helloGeeks = new Promise((resolve, reject) => {
   setTimeout(resolve, 120000, 'Hello Geeks!')
})
  
helloGeeks.then((value) => {
   console.log(value)
})


Output:

 

Approach 1: Let’s create a function ‘defaultResponse’ which in response returns a promise. We want to execute this function instead of the function which takes more time than usual, in this case, it is ‘helloGeeks’ function which takes more than 1 minute to respond.

Javascript




let defaultResponse = new Promise((resolve, reject) => {
    setTimeout(resolve, 60000, 'Response timeout!');
})


Now, we have to use ‘Promise.race’ method to race the promise, this method takes different promises as arguments and executes the one which takes less time, so we pass both ‘helloGeeks’ and ‘defaultResponse’ and it executes the ‘helloGeeks’ if it takes less than 1 minutes otherwise it executes the ‘defaultResponse’ since it takes 1 minutes only.

Javascript




Promise.race([helloGeeks, defaultResponse]).then((value) => {
   console.log(value);
});


Example: This is the complete running code using the above approach to solving the problem.

Javascript




let helloGeeks = new Promise((resolve, reject) => {
    setTimeout(resolve, 120000, 'Hello Geeks!')
})
  
let defaultResponse = new Promise((resolve, reject) => {
    setTimeout(resolve, 60000, 'Response timeout!');
})
  
Promise.race([helloGeeks, defaultResponse]).then((value) => {
    console.log(value);
});


Output:

 

Approach 2: Let’s create a function ‘defaultResponse’ which executes the log statement after 1 min. We want to execute this function instead of the function which takes more time than 1 min, in this case, it is the ‘helloGeeks’ function that takes more than 1 minute to respond. If the ‘helloGeeks’ function takes more time, then  ‘defaultResponse’ function gets executed and the flow of execution is stopped.

Example: This is the complete running code using the above approach to solving the problem.

Javascript




let defaultResponse = () => {
    setTimeout(() => {
        console.log('Response timeout!');
        process.exit();
    }, 2000);
}
  
let helloGeeks = () => {
    setTimeout(() => {
        console.log('Hello Geeks!');
    }, 6000);
}
  
defaultResponse();
helloGeeks();


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads