export interface PromiseTimeoutFunction { /** * @see timeout * @param time Time to resolve promise in miliseconds * @param value Value to resolve promise with * @param key Key to cancel the timeout if needed */ (time: number, value?: T, key?: symbol): Promise; /** * #### Promise Timeout Canceller * * * * * * Usage via promise instance: * ```typescript * import { timeout } from "@thalesrc/js-utils/promise"; * * const timeout = timeout(1000); * * timeout * .then(() => console.log("this won't be logged")) * .catch(() => console.log("this will be logged, because timer has been cancelled")); * * timeout.cancel(timeout); * ``` * * Usage via key * ```typescript * import { timeout } from "@thalesrc/js-utils/promise"; * * const key = Symbol(); * * timeout(1000, null, key) * .then(() => console.log("this won't be logged")) * .catch(() => console.log("this will be logged, because timer has been cancelled")); * * timeout.cancel(key); * ``` * Static usage example: * ```typescript * import "@thalesrc/js-utils/promise/static/timeout"; * * const timeout = Promise.timeout(1000); * * timeout * .then(() => console.log("this won't be logged")) * .catch(() => console.log("this will be logged, because timer has been cancelled")); * * Promise.timeout.cancel(timeout); * ``` * * * * * @param identifier The identifier of the promise to cancel * @param error The error which will be throwed by the cancelled promise * @returns A promise which resolves if cancelling process is successfull, rejects otherwise */ cancel(identifier: Promise | symbol, error?: unknown): Promise; /** * Will be throwed in cancelling promise when the timer has already finished or cancelled */ readonly FINISHED_ALREADY: unique symbol; /** * Will be throwed in cancelling promise when the timer has not found via identifier */ readonly IDENTIFIER_NOT_FOUND: unique symbol; /** * The default timeout cancelling rejection error * Will be throwed when the timer has cancelled */ readonly TIMEOUT_CANCELLED: unique symbol; } /** * #### Promise Timeout * Returns a promise which resolves after given time * * * * * * Example: * ```typescript * import { timeout } from "@thalesrc/js-utils/promise"; * * timeout(1000); * .then(() => console.log("will be logged after a second")); * ``` * Example with a resolve value: * ```typescript * import { timeout } from "@thalesrc/js-utils/promise"; * * timeout(1000, "foo"); * .then(val => console.log("will log 'foo' after a second", val)); * ``` * Can be used in promise chaining: * ```typescript * import { timeout } from "@thalesrc/js-utils/promise"; * * fetch("http://localhost:8080/anEndpoint") // Fetch something * .then(val => timeout(1000, val)) // Wait a second after response * .then(val => { * ...do something else * }); * ``` * Static usage example: * ```typescript * import "@thalesrc/js-utils/promise/static/timeout"; * * Promise.timeout(1000); * .then(() => console.log("will be logged after a second")); * ``` * * * * * @see PromiseTimeoutFunction#cancel for cancelling */ export declare const timeout: PromiseTimeoutFunction;