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) {
    console.error(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.



Read more here: https://stackoverflow.com/questions/66263021/timeout-async-await-functions-after-a-few-seconds

Content Attribution

This content was originally published by Kauan Polydoro at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: