Skip to content

Latest commit

 

History

History
174 lines (109 loc) · 4.54 KB

reducers.md

File metadata and controls

174 lines (109 loc) · 4.54 KB

Reducers

Reducers pull and process each value produced by the iterable. Although they can have any parameters, and output anything, the last parameter must be an iterable.

This documentation is for the functional API, although sometimes the examples use the chainable API for now.

Table of Contents

forEach

Executes function fn(item, index) for each item in the iterable sequence provided. Each function call must resolve before the function is called again.

  • fn (AsyncFunction | Function) a function(item, index), where item is the current item in the sequence, and index is the index of the current item.
  • iterable Iterable the sequence of items to call fn(item, index) with.
const fn = (item, index) => console.log(`item - ${item}, index - ${index}`)
forEach(fn, [1, 2, 3]) // prints the following...
// item - 1, index - 0
// item - 2, index - 1
// item - 3, index - 2

Returns undefined

publish

Publishes values from iterable to any subscribing function. Functions can be added and removed during iteration.

Functions are passed the each value from the iterable. Only values provided after subscribing are provided. Any return value is ignored.

  • iterable (AsyncIterable | Iterable) that provides the values
const publisher = chainable([0, 1, 2, 3]).throttle(50, 50).publish()
const key = publisher.subscribe(console.log) // prints 0, 1 - then unsubscribed
wait(110, publisher.unsubscribe(key)) // console.log is no longer called after 110ms

Returns Object Returns an object with subscribe and unsubscribe methods. The subscribe method accepts a function and returns an unsubscribe key. The unsubscribe method accepts an unsubscribe key, to remove the function. Also has start, stop, and running just like the run function.

reduce

The reduce() method executes a reducer function on each element of the input iterable, resulting in a single output value.

  • fn (AsyncFunction | Function) fn(accumulator, item) that returns (or resolves to) the new accumulator value.
  • accumulator any the initial accumulator value
  • iterable Iterable the sequence to execute fn over.
const add = (a, b) => a + b
const sum = reduce(add, 0, [0, 1, 2, 3, 4]) // sum === 10

Returns any the final accumulator value

run

Iterates over an iterable asynchronously. It is sort of like starting a thread, and the returned controller allows you to start and stop iteration.

  • iterable (AsyncIterable | Iterable) input iterable
const control = run(longRunningIterable)
if (control.running) control.stop()
control.start()

Returns Object Has a start() method to start iteration, although it is already started when controllable returns. Has a stop method to stop iteration. Has a running attribute to check if iteration is running.

runAwait

Iterates over iterable asynchronously. It is sort of like starting a thread. Unlike run(iterable), there is no control over iteration.

  • iterable (AsyncIterable | Iterable) iterable to iterate over
await runAwait([0, 1, 2, 3, 4])

toArray

Creates an Array from the items in iterable.

  • iterable (AsyncIterable | Iterable) the iterable to create the array from
const a = await toArray([0, 1, 2, 3, 4]) // a is [0, 1, 2, 3, 4]

Returns Array the array