Skip to content

timeout

awekrx edited this page May 29, 2026 · 1 revision

timeout

Import

import { timeout } from '@dev-suite/decorators/timeout'

Category

  • method

Use Case

Fail long-running async methods after a fixed time budget.

Replaces

  • Promise.race timeout wrappers in each method
  • Custom watchdog utilities in service code

Example 1

Without decorator

class FileService {
  async upload(file: Buffer) {
    return Promise.race([
      this.remote.upload(file),
      new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 5000)),
    ]);
  }
}

With decorator

import { timeout } from '@dev-suite/decorators/timeout';

class FileService {
  @timeout({ ms: 5000, message: 'Upload timeout' })
  async upload(file: Buffer) {
    return this.remote.upload(file);
  }
}

Why better

  • Timeout behavior is consistent across methods.
  • Eliminates repeated Promise.race code.

Example 2

Without decorator

class ReportService {
  async build(id: string) {
    const task = this.engine.build(id);
    const timeoutGuard = new Promise((_, reject) => setTimeout(() => reject(new Error('build timeout')), 2000));
    return Promise.race([task, timeoutGuard]);
  }
}

With decorator

import { timeout } from '@dev-suite/decorators/timeout';

class ReportService {
  @timeout({ ms: 2000 })
  async build(id: string) {
    return this.engine.build(id);
  }
}

Why better

  • Simple to tune per method with one option change.
  • Keeps method logic focused on work, not guards.

Clone this wiki locally