Module
Re-execute a function until it does not throw an error or execute a function with a timeout.
Very Popular
Latest
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156// Copyright since 2020, FranckLdx. All rights reserved. MIT license.import { deferred, denoDelay } from "../deps.ts";import { assertEquals, assertThrowsAsync } from "../dev_deps.ts";import { isTimeoutError, TimeoutError } from "./timeoutError.ts";import { waitUntil, waitUntilAsync } from "./wait.ts";
Deno.test({ name: "waitAsync return function return code", fn: async () => { const delay = 1000; const result = Symbol("OK"); const fn = () => { const p = deferred<typeof result>(); p.resolve(result); return p; }; const actualResult = await waitUntilAsync(fn, delay); assertEquals(actualResult, result); await denoDelay(delay); },});
Deno.test({ name: "waitAsync throw function exception", fn: async () => { const delay = 1000; const errorMsg = "BOOM"; const fn = () => { throw new Error(errorMsg); }; await assertThrowsAsync( async () => await waitUntilAsync(fn, delay), Error, errorMsg, ); await denoDelay(delay); },});
Deno.test({ name: "waitAsync throw TimeoutError exception", fn: async () => { const delay = 1000; const fn = () => { return deferred(); }; const error = await assertThrowsAsync( async () => await waitUntilAsync(fn, delay), TimeoutError, "function did not complete within allowed time", ); assertEquals((error as TimeoutError).isTimeout, true); await denoDelay(delay); },});
Deno.test({ name: "waitAsync throw custom Error exception", fn: async () => { const delay = 1000; const fn = () => { return deferred(); }; const errorMsg = "BOOM"; const error = await assertThrowsAsync( async () => await waitUntil(fn, delay, new Error(errorMsg)), Error, errorMsg, ); // deno-lint-ignore no-explicit-any assertEquals((error as any).isTimeout, undefined); await denoDelay(delay); },});
Deno.test({ name: "wait return function return code", fn: async () => { const delay = 1000; const result = Symbol("OK"); const fn = () => result; const actualResult = await waitUntil(fn, delay); assertEquals(actualResult, result); await denoDelay(delay); },});
Deno.test({ name: "wait throw function exception", fn: async () => { const delay = 1000; const errorMsg = "BOOM"; const fn = () => { throw new Error(errorMsg); }; await assertThrowsAsync( async () => await waitUntil(fn, delay), Error, errorMsg, ); await denoDelay(delay); },});
Deno.test({ name: "wait throw TimeoutError exception", fn: async () => { const delay = 1000; const fn = () => { return deferred(); }; const error = await assertThrowsAsync( async () => await waitUntil(fn, delay), TimeoutError, "function did not complete within allowed time", ); assertEquals((error as TimeoutError).isTimeout, true); await denoDelay(delay); },});
Deno.test({ name: "wait throw custom Error exception", fn: async () => { const delay = 1000; const fn = () => { return deferred(); }; const errorMsg = "BOOM"; const error = await assertThrowsAsync( async () => await waitUntil(fn, delay, new Error(errorMsg)), Error, errorMsg, ); // deno-lint-ignore no-explicit-any assertEquals((error as any).isTimeout, undefined); await denoDelay(delay); },});
Deno.test({ name: "isTimeoutError should return true for timeout error", fn: () => { const error = new TimeoutError("bla bla bla"); assertEquals(isTimeoutError(error), true); },});
Deno.test({ name: "isTimeoutError should return false for non timeout error", fn: () => { const error = new Error("bla bla bla"); assertEquals(isTimeoutError(error), false); },});