Timeout async/await functions after a few seconds

I created a function to get an Oracle connection using oracledb, but in a few cases, oracledb didn’t throw an exception even if he didn’t get the connection, apparently he’s trying to connect infinitely.

const getConnection = async (): Promise<oracledb.Connection | undefined> => {
  let connection;

  try {
    connection = await oracledb.getConnection({
      user: process.env.DB_LOGIN,
      password: process.env.DB_PASSWORD,
      connectString: process.env.DB_STRING_CONNECTION,
  } catch (err) {
  return connection;

I saw a few samples using Promise.racing with setTimeout but i couldn’t put it in pratice, my tries always get an UnhandledPromiseRejectionWarning on console so i think it isn’t the correct way.

Can anybody show me an example how to do that?

I’m learning JavaScript/TypeScript, sorry if this is a stupid question, but i really appreciate any help

Thanks in advance.

50 thoughts on “Timeout async/await functions after a few seconds”

  1. 274850 711567As far as me being a member here, I wasnt aware that I was a member for any days, really. When the post was published I received a notification, so that I could participate within the discussion of the post, That would explain me stumbuling upon this post. But were certainly all members inside the world of tips. 533451


Leave a Comment