0.1.2 • Published 3 years ago

@haiix/aseq v0.1.2

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

aseq.mjs

A JavaScript module that allows asynchronous iterators to use array-like methods.

There is also a synchronous version: https://github.com/haiix/seq

Installation

npm install @haiix/aseq

Usage

import aseq from '@haiix/aseq'

Examples

  1. By passing the natural number N to the aseq function, an async iterator is created that increments between 0 and N. You can use array-like methods (map, join, etc.) on the created async iterator.

    await aseq(3).join() // "0,1,2"
    await aseq(4).map(x => x + 1).join() // "1,2,3,4"
  2. You can pass an async iterable object (with the Symbol.asyncIterator method) to the aseq function.

    async function* myFunc () {
      for (let n = 10; n <= 30; n += 10) {
        await new Promise(resolve => setTimeout(resolve, 100))
        yield n
      }
    }
    await aseq(myFunc()).map(x => x + 1).join() // "11,21,31"
  3. There are no methods implemented that rewrite themselves. If you want to use those methods, convert them to arrays once. You can use the toArray method for that.

    //aseq(5).reverse() // Error
    (await aseq(5).toArray()).reverse() // [4, 3, 2, 1, 0]
  4. In this example, the value is returned when the result of indexOf is found, without waiting for the original processing to finish.

    await aseq(100).map(x => x * 2).filter(x => x % 3 > 0).indexOf(16) // 5
  5. The iterator created by the aseq function can be used in a for-await-of statement. Use in processing with side effects.

    for await (const n of aseq(3)) {
      console.log(n) // 0, 1, 2
    }
    for await (const elem of aseq(3).map(n => document.createElement('input'))) {
      document.body.appendChild(elem)
    }
0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago