await-at-least
v1.0.0
Published
A small set of functions to await at least a certain amount of time before resolving a promise.
Downloads
16
Maintainers
Readme
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.