import { GetterP } from '../Getter' import { MapperP, PredicateP } from '../Mapper' import { ParserP } from '../Parser' export const maxDefault = 10 export const getUntilIsValidP = (get: GetterP, isValid: PredicateP) => async (max: number = maxDefault): Promise => { if (max <= 0) return const value = await get() if (await isValid(value)) return value return getUntilIsValidP(get, isValid)(max - 1) } export const getUntilParseP = (get: GetterP, parse: ParserP) => async (max: number = maxDefault): Promise => { if (max <= 0) return const value = await get() const result = await parse(value) if (result.success) { return result.data } else { return getUntilParseP(get, parse)(max - 1) } } export const getUntilMap = (get: GetterP, map: MapperP) => async (max: number = maxDefault): Promise => { if (max <= 0) return try { const value = await get() return map(value) } catch (e) { return getUntilMap(get, map)(max - 1) } }