UNPKG

@lou.codes/iterables

Version:
19 lines (18 loc) 554 B
import { awaitableHandler } from "@lou.codes/utils"; import { getIterator } from "./getIterator.js"; /** * Get first element of an iterable or asynchronous iterable (`undefined` if * it is empty). * * @category Asynchronous Reducers * @example * ```typescript * head([1, 2, 3]); // 1 * ``` * @param iterable Iterable to get the first element from. * @returns First element of the iterable (`undefined` if empty). */ export const head = iterable => awaitableHandler(iteratorResult => iteratorResult.value)( getIterator(iterable).next(), );