Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/future.
Something went wrong. Try again.
future split.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import type { DualDisposable } from "./types.ts";import type { Future } from "./future.ts";
import { splitIter, splitIterBy } from "./_iter.ts";import { fromIterator } from "./from.ts";
/** * Splits a generator-based Future into separate success and error streams. * * The async generator foundation enables this capability: as the generator yields and potentially * throws, the split function routes values to different output generators. Each output is itself * a Future, so you can iterate, cancel, or compose them independently. * * @param future - Generator-based Future to split * @returns Tuple of [resolvedFuture, erroredFuture], both generators * * @example Separate error handling * ```typescript * const future = from(async function* () { * yield 1; * yield 2; * throw new Error("Failed"); * }); * * const [resolved, errors] = split(future); * * // Handle successes * for await (const value of resolved) { * console.log('Success:', value); // 1, 2 * } * * // Handle errors separately * for await (const error of errors) { * console.error('Error:', error); * } * ``` */export function split<V, E, TReturn = unknown>( future: Future<V, TReturn>,): readonly [Future<V, undefined>, Future<E, undefined>] & DualDisposable { const _split = splitIter<V, E, TReturn>(future);
const [resolvedIterator, erroredIterator] = _split; return Object.assign( [ fromIterator<V, undefined>(resolvedIterator), fromIterator<E, undefined>(erroredIterator), ] as const, { [Symbol.dispose]() { _split[Symbol.dispose](); }, [Symbol.asyncDispose]() { return _split[Symbol.asyncDispose](); }, }, );}
/** * Splits a Future into two futures based on a predicate. * * @param future The original future to be split. * @param predicate The function that determines which values go to the first future. * @returns An array of two futures: one for values that satisfy the predicate and one for values that do not. * * @example * ```ts * const future = Future.from(async function* () { * yield 1; * yield 2; * yield 3; * yield 4; * }); * * const isEven = (value: number) => value % 2 === 0; * * const [evenFuture, oddFuture] = Future.splitBy(future, isEven); * * evenFuture.then(console.log); // Logs 2, 4 * oddFuture.then(console.log); // Logs 1, 3 * ``` */export function splitBy<T, F, VReturn = unknown>( future: Future<T | F, VReturn>, predicate: (value: T | F) => boolean | PromiseLike<boolean>,): & readonly [Future<T, VReturn | undefined>, Future<F, VReturn | undefined>] & DualDisposable { const _split = splitIterBy<T, F, VReturn>( future, predicate, );
const [matchedIterator, nonMatchedIterator] = _split; return Object.assign( [ fromIterator<T, undefined>(matchedIterator), fromIterator<F, undefined>(nonMatchedIterator), ] as const, { [Symbol.dispose]() { _split[Symbol.dispose](); }, [Symbol.asyncDispose]() { return _split[Symbol.asyncDispose](); }, }, );}