Skip to content

head() function

返回 Iterable/AsyncIterable 的第一个元素。

There is another alias function called first.

Signature:

typescript
declare function head<T extends Iterable<unknown> | AsyncIterable<unknown>>(
  iterable: T,
): HeadReturnType<T>;

Example

ts
head([1, 2, 3, 4, 5]); // 1

// with pipe
pipe([1, 2, 3, 4, 5], head); // 1

await pipe(Promise.resolve([1, 2, 3, 4, 5]), head); // 1

// with toAsync
await pipe(
  [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)],
  toAsync,
  head,
); // 1

Try It

see pipe, toAsync

Open Source Code

Released under the Apache-2.0 License.