Skip to content

Async versions of javascript array methods: every, filter, find, findIndex, some

Notifications You must be signed in to change notification settings

samuelkarani/arrays-sugar

Repository files navigation

arrays sugar

What's this package?

Async versions of array functions with predicate callbacks e.g.

import { findIndex } from 'arrays-sugar'

const array = [1, 2, 3];
findIndex(array, async (number) => number === 2) // 1 ✅
array.findIndex(async (number) => number === 2) // 0 ❌

Built with Typescript for Node.js or the Browser.

What's in it?

A set of purely functional array methods with async predicates: every, filter, find, findIndex, some

These are used internally in the related AI Sugar package/library. Check it out especially if you're building or working with AI in Typescript/Node.js.

So each function has 2/3 versions:

  • default version which is concurrent (uses Promise.allSettled internally) and is also available with the suffix Concurrent
   // both are equivalent
   [1, 2, 3].every(async (number) => number === 2) === true
   [1, 2, 3].everyConcurrent(async (number) => number === 2) === true
  • optimized version that may not iterate the entire array on condition that the predicate throws for falsy values (instead of returning false). This is available for all except filter which has to go through the whole array. Uses the suffix Optimized.
    const result = await findOptimized(array, async (item) => {
      if (item <= 1) {
        throw new Error("error");
      }
      return true;
    });
  • serial version that iterates one at a time (slow). Uses the suffix Serial.
    const result = await findIndexSerial(array, async (item) => {
      await sleep(100);
      return item > 1;
    });

Why?

Using callbacks with promises or async/await inside findIndex always returns truthy for all items in the array.

If you try this in the console or node.js it will always return false

[1, 2, 3].findIndex(async (number) => number === 2) === 1

The same goes for every, find, filter, some

Outside map it seems only reduce can work with promise or async/await callbacks

const sum = await [1, 2, 3].reduce((accumulator, number) => {
  return accumulator.then(value => value + number)
}, Promise.resolve(0));
console.log(sum) // 6

Coming soon

Async versions of:

  1. indexOf
  2. lastIndexOf
  3. findLast
  4. findLastIndex

That's it!

Thanks for reading. I welcome your input, suggestions, feedback. Here is the medium article I wrote introducing this library.

Check out the following related libraries that I also built with this release.

ai-sugar AI Sugar is a collection of utility functions for working with AI apis.

const sorted = await ai.sort({
  array: ["green", "red", "blue", "yellow"],
  prompt: "rainbow color order",
});
// ["red", "yellow", "green", "blue"]

zod-sugar Zod Sugar is basically reverse zod i.e. creates a zod schema from a javascript value:

const object = { foo: "bar", baz: 1 };
const schema = createZod(object);
// z.object({ foo: z.string(), bar: z.number() });
schema.safeParse(object).success // true

What I'm building

Similarly logo

Find the best alternatives with one click. Discover similar websites, tools and services instantly while browsing. Never miss out on better options again. Check out Similarly

About

Async versions of javascript array methods: every, filter, find, findIndex, some

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published