Methods
(static) fromStream(eventSource, dataEventopt, closeEventopt) → {iteration}
- Source:
import {fromStream} from 'async_iter/pipeline/from_stream' # pipeline version
import {fromStream} from 'async_iter/from_stream' # conventional version
Returns an iterator, that emits as per the dataEvent
of the eventSource
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
eventSource |
EventEmitter | An object that supports the |
||
dataEvent |
String |
<optional> |
data
|
The main dataEvent name to listen to |
closeEvent |
String |
<optional> |
close
|
When this event emits, the iteration is stopped |
Returns:
An iterable source
- Type
- iteration
(static) interval(period, cancel) → {Iterable}
- Source:
import {interval} from 'async_iter/interval'
Returns an async iterator the will emit every period
milliseconds
- The iterator will block its emitted values, until the consumer has consumed the item, therefore there is no racing of producer to consumer.
- The iteration stops, when the consumer breaks, stop the iteration or the cancel promise is resolved
Parameters:
Name | Type | Description |
---|---|---|
period |
Number | the time in milliseconds the iterator emits |
cancel |
Promise | A promise that when it resolves, causes the interval to stop generating values |
Returns:
An iteration that emits as per the interval period
- Type
- Iterable
(static) pump(fn, marker=opt) → {Iteratable}
import {pump} from 'async_iter/pump'
pump allows for the 'pushing' of values into an async iterator consumer
The push
operation returns a promise, that resolves when the consuming iteration has consumed the item
This function follows the convention of a pushed iterator interface (next, throw, return).
If the code pushing values, does not await the return promise, the values are then queued for processing by the consumer as it pulls in the values
The callback is not invoked, until the first item is pulled from the iteration
Example
import {pump} from 'async_iter'
// Create a push based iteration set
const items = await pump(target => {
//Values can be push to the iteration
await target.next(1) // if you dont 'await' the values will be queued.
await target.next(2)
await target.next(3)
// If you want to push an 'error' to the consumer
// await target.throw(new Error('This is an error'))
await target.return()
})
for await (const item of items)
console.log(item)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn |
pumpCallback | this is a function that will async pump values into the interator |
|
marker= |
String |
<optional> |
Returns:
A standard async iterator that can consume the generated values
- Type
- Iteratable
(static) range(optsopt) → {Iterable}
import {range} from 'async_iter/range' # conventional version
Returns an iterator that iterates from start to end (inclusive) by step amounts
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opts |
Object |
<optional> |
the optional start, end and step values for the generation Properties
|
Returns:
An iteration for the specified range
- Type
- Iterable
(static) spawn(command, argsopt, optionsopt) → {iteration}
Adapter for the nodejs function child_process.spawn
. Mapping the stdout and stderr to an iterable
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
command |
string | The command to run |
|
args |
Array.<string> |
<optional> |
The arguments to supply to the command |
options |
object |
<optional> |
Any options to be passed thru to child_process.spawn |
Returns:
An iterable source that contain an object with one of 2 keys (stdout or stderr)
- Type
- iteration