1.0.0 • Published 9 months ago

await-at-least v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

await-at-least

A small set of functions to await at least a certain amount of time before resolving a promise.

Installation

npm install await-at-least

Usage

awaitAtLeast

Wraps the given promise and makes sure it takes at least the given duration before it resolves or rejects.

import { awaitAtLeast } from "await-at-least";

const postToServer = async (url: string, body: unknown) => {
  // ...
};

const responseFromServer = await awaitAtLeast(
  800,
  postToServer("https://api.example.com", requestBody)
);

If the given promise resolves before the given amount of time is up, the returned promise will wait for the remaining time before returning the result.

If the given promise rejects before the given amount of time is up, the returned promise will wait for the remaining time before rethrowing the error.

awaitAtLeastOrReject

Wraps the given promise and makes sure it takes at least the given duration before it resolves but will reject as soon as the wrapped promise rejects.

import { awaitAtLeastOrReject } from "await-at-least";

const postToServer = async (url: string, body: unknown) => {
  // ...
};

const responseFromServer = await awaitAtLeastOrReject(
  800,
  postToServer("https://api.example.com", requestBody)
);

If the given promise resolves before the given amount of time is up, the returned promise will wait for the remaining time before returning the result.

If the given promise rejects before the given amount of time is up, the returned promise will reject immediately.

License

await-at-least is licensed under the ISC license.

1.0.0

9 months ago