GithubHelp home page GithubHelp logo

ziggy42 / async-array Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 671 KB

AsyncArray is a class that inheriths from Array and adds async methods with support for async callbacks such as asyncForEach, asyncMap etc

License: Apache License 2.0

JavaScript 100.00%
javascript array async

async-array's Introduction

async-array

AsyncArray is a class that inheriths from Array and adds async methods with support for async callbacks such as asyncForEach, asyncMap etc:

const existing = await AsyncArray.of(1, 2, 3)
    .asyncFilter(async (id) => existsInDatabase(id))

Installation

npm install @ziggy42/async-array

Usage

The constructor of AsyncArray behaves exactly like Array. From MDN:

A JavaScript array is initialized with the given elements, except in the case where a single argument is passed to the Array constructor and that argument is a number.
If the only argument passed to the Array constructor is an integer between 0 and 232-1 (inclusive), this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values). If the argument is any other number, a RangeError exception is thrown.

const arr1 = new AsyncArray(1, 2, 3) // AsyncArray [ 1, 2, 3 ]

const arr2 = new AsyncArray(3) // AsyncArray [ <3 empty items> ]

There are also two static functions to create new AsyncArray instances:

const arr1 = AsyncArray.of(1, 2, 3) // AsyncArray [ 1, 2, 3 ]

const arr2 = AsyncArray.from([1, 2, 3]) // AsyncArray [ 1, 2, 3 ]

A regular Array can be obtained form an AsyncArray:

const arr = AsyncArray.of(1, 2, 3).toArray() // [ 1, 2, 3 ]

AsyncArray adds the following methods:

  • asyncEvery
  • asyncFilter
  • asyncFind
  • asyncFindIndex
  • asyncFlatMap
  • asyncForEach
  • asyncMap
  • asyncReduce
  • asyncReduceRight
  • asyncSome

These methods behave exactly like the non-async versions but they accept async callbacks.

const existing = await AsyncArray.of('1', '2', '3')
    .asyncFilter(async (id) => existsInDatabase(id))

Be aware that the async callbacks are executed in sequence, NOT in parallel. The following methods allow you to execute the callbacks in parallel instead:

  • asyncFilterAll
  • asyncMapAll
const existing = await AsyncArray.of(1, 2, 3)
    .asyncFilterAll(async (id) => existsInDatabase(id))

async-array's People

Contributors

ziggy42 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.