Skip to content

Latest commit

 

History

History
46 lines (33 loc) · 1.06 KB

promise.md

File metadata and controls

46 lines (33 loc) · 1.06 KB

Promise Module

Offers a set of utility functions to handle Promises, including sleep, retry and throttling.

Refer to PromiseService for further details.


Usage

Import PromiseModule at your application, followed by injecting PromiseService at your target provider:

import { PromiseService, HttpService } from '@bechara/crux';

@Injectable()
export class FooService {

  public constructor(
    private readonly promiseService: PromiseService,
    private readonly httpService: HttpService,
  ) { }

  public async readFooOrTimeout(): Promise<unknown> {
    const timeout = 5000; // 5 seconds
    return this.promiseService.resolveOrTimeout({
      promise: () => this.httpService.get('foo'),
      timeout
    });
  }

  public async readFooWithRetry(): Promise<unknown> {
    return this.promiseService.retryOnRejection({
      method: () => this.httpService.get('foo'),
      retries: 5,
      timeout: 2 * 60 * 1000, // 2 minutes,
      delay: 500, // 500 ms
    });
  }

}

Back to title