0

I am currently trying to make it check for a database connection. but it seems like the result, ie connection is pending.

I am looking to impliment a system where i can send an timeout input, where the promise would be rejected due to a fixed timeout.

Something like;

try {
start(timeout: 6000) // 6 secs timeout on promise. default, ie no params: 3sec
} catch(e) {
// failed due to, in this case, timeout, since there is no connection running, and the database is pending their promise.

}

How can i accomplish such timeout?

current running example gives the following:

connected to mongo Promise { <pending> }

starting server code:

const start = async () => {
    console.log("connecting to database");
    try {
        console.log("Attempting to establish connection....");
        var result = await mongoose.connect('mongodb://localhost/db',{useNewUrlParser: true});
        console.log("connected to mongo",result);
        return result;
    } catch (error) {
        console.log("failed to connect to mongo",error);
        
    }
    }
  



try {
    start();
} catch(e) {
    console.log("failed to start server",e);
    throw new Error("failed to start server",e);

}
maria
  • 843
  • 5
  • 18
  • 52

0 Answers0