Creating timeout for promise in JS

Gist for creating a timeout mechanism for a promise in JS

When I have a promise that may fail but doesn’t error out, I didn’t know to create a timeout mechanism in TS

Given a promise p that may take a very long time, create a race with another promise with a fixed execution time

const p = await someDatabase.select().from(schema.giantTable);

const r = await Promise.race([
  p,
  new Promise((res) => setTimeout(() => res("timeout exceeded"), 10000)),
]);
Hari Anugrah