UNPKG

neverthrow-result-utils

Version:

Extra utilities for working with Neverthrow results

74 lines 4.4 kB
import { ResultAsync } from "neverthrow"; export declare class ResultUtils { /** * Given a list of ResultAsync, it will return the result only from the * first that completes. * @param asyncResultList * @returns */ static race<T, E>(asyncResultList: ResultAsync<T, E>[]): ResultAsync<T, E>; /** * Executes a list of functions that return ResultAsync one at a time, * rather than in parrallel. Semantically similar to combine, but the * order or execution is different. Instead of taking an array of ResultAsync * like combine(), it takes a list of generator functions that return ResultAsync. * The functions will be executed sequentially, and the results will be returned in * the order they were executed. * @param funcList * @returns */ static executeSerially<T, T2, T3, E, E2, E3>(funcList: [ () => ResultAsync<T, E>, () => ResultAsync<T2, E2>, () => ResultAsync<T3, E3> ]): ResultAsync<[T, T2, T3], E | E2 | E3>; static executeSerially<T, T2, E, E2>(funcList: [() => ResultAsync<T, E>, () => ResultAsync<T2, E2>]): ResultAsync<[T, T2], E | E2>; static executeSerially<T, E>(funcList: (() => ResultAsync<T, E>)[]): ResultAsync<T[], E>; /** * backoffAndRetry() is a powerful method that executes a function that returns * a ResultAsync. If that result is OK, then the result is returned directly. * If it is an error, it will check to see if the returned error is in the * acceptableErrors list. If so, it will delay and re-execute the function. * If it's not an acceptable error type, the error result is returned. * Each failure will backoff exponentially. * * This is an excellent wrapper for functions that can fail inconsistently * in some ways were we expect a retry to succeed. For example, a function * that communicates with a database over a sketchy connection might just need * to retry in a few seconds. * @param func * @param acceptableErrors A list of error types that are considered retryable. Logical error types that will never succeed should not be in this list, but error types that represent temporary runtime errors should. * @param maxAttempts This is the maximum number of times a retry will be attempted; set to null to use infinite retries. * @param baseSeconds This is the delay in seconds for the first retry attempt. It will double with each successive retry. * @returns */ static backoffAndRetry<T, E extends Error>(func: () => ResultAsync<T, E>, acceptableErrors: Function[], maxAttempts?: number, baseSeconds?: number): ResultAsync<T, E>; /** * filter() is a normal filter method that works with async callbacks. * This works like a nomral array filter() call, except the callback returns * a ResultAsync<boolean> instead of just boolean. * @param arr the source array * @param callback a function that returns a ResultAsync<boolean>; if it returns true then the source value is included in the result. * @returns a ResultAsync containing an array of source values where the callback returns true. */ static filter<T, E extends Error>(arr: T[], callback: (val: T) => ResultAsync<boolean, E>): ResultAsync<T[], E>; /** * map() is a way to combine multiple async callbacks. * This works like a normal array map() call, except the callbacks return ResultAsync. * It will combine all the results and return a single ResultAsync with an array of the mapped * values. * This is just a handy way to do ResultAsync.combine(values.map((val) => okAsync(val))). * @param arr The source array of objects * @param callback A function that returns a ResultAsync and a value * @returns A ResultAsync containing an array of the mapped values or the first error. */ static map<T, U, E extends Error>(arr: T[], callback: (val: T) => ResultAsync<U, E>): ResultAsync<U[], E>; /** * This is a ResultAsync version of the delay() function from the delay package. It lets * you use Neverthrow semantics for delays. * @param ms The number of milliseconds to delay for. * @returns */ static delay(ms: number): ResultAsync<void, never>; } //# sourceMappingURL=index.d.ts.map