Gathering detailed insights and metrics for create-async-generator
Gathering detailed insights and metrics for create-async-generator
Gathering detailed insights and metrics for create-async-generator
Gathering detailed insights and metrics for create-async-generator
quansync
Create sync/async APIs with usable logic
@postinumero/use-async
Create a suspending hook from an async function, an async generator or a function that returns an async iterator.
@async-generator/subject
create an async iterator with functions to feed value into and end it
async-generator
A helper to create async generators
Create an async generator out of anything and pipe the output
npm install create-async-generator
Typescript
Module System
Node Version
NPM Version
74.2
Supply Chain
98.9
Quality
75.7
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
1,434
Last Day
2
Last Week
14
Last Month
43
Last Year
247
MIT License
15 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Aug 04, 2022
Latest Version
2.0.0
Package Id
create-async-generator@2.0.0
Unpacked Size
60.64 kB
Size
9.26 kB
File Count
32
NPM Version
8.19.4
Node Version
16.20.2
Published on
Dec 16, 2023
Cumulative downloads
Total Downloads
Last Day
100%
2
Compared to previous day
Last Week
75%
14
Compared to previous week
Last Month
19.4%
43
Compared to previous month
Last Year
-26.5%
247
Compared to previous year
1
9
1
Create an async generator out of any value and pipe async generators into each other.
npm i create-async-generator
From will convert any value into an async generator.
Passing a promise converts it into an async generator that will yield the promise result
1import { from } from 'create-async-generator'; 2 3const input = Promise.resolve(1); 4const generator = from(input); 5 6for await (const output of generator()) { 7 console.log(output); //=> 1 8}
Passing an async iterable will yield all of its values
1import { from } from 'create-async-generator'; 2 3async function* inputGenerator(value) { 4 yield value * 2; 5 yield value * 3; 6 yield value * 4; 7} 8 9const input = inputGenerator(2); 10 11const generator = from(input); 12 13for await (const output of generator()) { 14 console.log(output); //=> 4, 6, 8 15}
Functions will be called with the generator arguments
1import { from } from 'create-async-generator'; 2 3async inputGenerator(value) { 4 // value will be the generator argument 5 return value * 2; 6} 7 8const generator = from(inputGenerator); 9 10for await (const output of generator(2)) { 11 console.log(output); //=> 4 12}
Generator functions will be called with the generator arguments
1import { from } from 'create-async-generator'; 2 3async function* inputGenerator(value) { 4 // value will be the generator argument 5 yield value * 2; 6 yield value * 3; 7 yield value * 4; 8} 9 10const generator = from(inputGenerator); 11 12for await (const output of generator(2)) { 13 console.log(output); //=> 4, 6, 8 14}
Any argument that is not a function, promise or async iterable will return a generator that will yield the passed argument.
1import { from } from 'create-async-generator'; 2 3const input = 1; 4const generator = from(input); 5 6for await (const output of generator()) { 7 console.log(output); //=> 1 8}
combineIterable
depends on the async-iterators-combine package.
Every item in the passed iterable will be converted to an async generator (using from) and yielded values will be combined into iterables.
For a list of all options see the async-iterators-combine docs
import { combineIterable } from 'create-async-generator';
const generator = combineIterable([
async function* (value) {
yield value * 2;
yield value * 3;
yield value * 4;
},
(value) => Promise.resolve(value * 10),
Promise.resolve('wow'),
'example',
]);
for await (const output of generator(2)) {
console.log(output); //=> [4, 20, 'wow', 'example'], [6, 20, 'wow', 'example'], [8, 20, 'wow', 'example']
}
combineObject
behaves exactly the same as combineIterable but accepts an object instead of an iterable and will yield objects.
import { combineObject } from 'create-async-generator';
const generator = combineObject({
a: async function* (value) {
yield value * 2;
yield value * 3;
yield value * 4;
},
b: (value) => Promise.resolve(value * 10),
c: Promise.resolve('wow'),
d: 'example',
});
for await (const output of generator(2)) {
console.log(output); //=> { a: 4, b: 20, c: 'wow', d: 'example' }, { a: 6, b: 20, c: 'wow', d: 'example' }, { a: 8, b: 20, c: 'wow', d: 'example' }
}
new YieldableAsyncGenerator()
creates an object that conforms to the async iterable and async iterator protocol and includes a yield
method to yield values.
When a signal is passed to the constructor (new YieldableAsyncGenerator({ signal })
) the generator will be returned as soon as the signal is aborted.
import { YieldableAsyncGenerator } from 'create-async-generator';
const generator = new YieldableAsyncGenerator();
await Promise.all([
(async () => {
for await (const output of generator) {
console.log(output) //=> 1, 2, 3
}
})(),
(async () => {
await new Promise((resolve) => setTimeout(resolve, 100));
generator.yield(1);
await new Promise((resolve) => setTimeout(resolve, 100));
generator.yield(2);
await new Promise((resolve) => setTimeout(resolve, 100));
generator.yield(3);
})(),
])
Extends YieldableAsyncGenerator and adds a share
method that will return a new object that conforms to the async iterable and async iterator protocol that will yield all values from the parent object but can be returned and thrown independently.
The share
method also takes an object with a signal as parameter (share({ signal })
) that will return the generator as soon as the signal is aborted.
import { ShareableAsyncGenerator } from 'create-async-generator';
const generator = new ShareableAsyncGenerator();
await Promise.all([
(async () => {
for await (const output of generator.share()) {
console.log(output) //=> 1, 2
if (output === 2) {
return;
}
}
})(),
(async () => {
for await (const output of generator.share()) {
console.log(output) //=> 1, 2, 3
}
})(),
(async () => {
for await (const output of generator.share()) {
console.log(output) //=> 1
if (output === 1) {
return;
}
}
})(),
(async () => {
await new Promise((resolve) => setTimeout(resolve, 100));
generator.yield(1);
await new Promise((resolve) => setTimeout(resolve, 100));
generator.yield(2);
await new Promise((resolve) => setTimeout(resolve, 100));
generator.yield(3);
})(),
])
## pipe
Will pipe the result of the first function as first argument in the following function, any arguments passed into the resulting function will be passed into the first function and as additional arguments into succeeding functions.
```js
import { pipe } from 'create-async-generator';
const generator = pipe(
async function* (value) {
yield value * 2;
yield value * 3;
yield value * 4;
},
async function* (generator, value) {
for await (const output of generator) {
yield output * 1 + value;
yield output * 2 + value;
yield output * 3 + value;
}
}
)
for await (const output of generator(2)) {
console.log(output); //=> 6, 10, 14, 8, 14, 20, 10, 18, 26
}
Operators return generator functions that can be used as arguments when calling pipe.
Map every yielded value to the value returned by the mapping method.
1import { pipe, map } from 'create-async-generator'; 2 3async function* inputGenerator(value) { 4 yield value * 2; 5 yield value * 3; 6 yield value * 4; 7} 8 9const generator = pipe( 10 inputGenerator, 11 map((value, i, originalValue) => value / 2 * i + originalValue), 12); 13 14for await (const output of generator(2)) { 15 console.log(output); //=> 2, 5, 10 16}
Will skip the yield if the value returned by the mapping method is falsy.
1import { pipe, filter } from 'create-async-generator'; 2 3async function* inputGenerator() { 4 yield 2; 5 yield 3; 6 yield 4; 7 yield 5; 8 yield 6; 9} 10 11const generator = pipe( 12 inputGenerator, 13 filter((value, i, _originalValue) => value % 2 === 0), 14); 15 16for await (const output of generator()) { 17 console.log(output); //=> 2, 4, 6 18}
Delegates the async iterable to the generator function that is returned from the map method.
1import { pipe, delegateMap } from 'create-async-generator'; 2 3async function* inputGenerator(value) { 4 yield value * 2; 5 yield value * 3; 6 yield value * 4; 7} 8 9const generator = pipe( 10 inputGenerator, 11 delegateMap((value, i, _originalValue) => value % 2 === 0 12 ? async function* (originalValue) { 13 yield originalValue * 2; 14 yield originalValue * 3; 15 yield originalValue * 4; 16 } : async function* (originalValue) { 17 yield originalValue * 20; 18 yield originalValue * 30; 19 yield originalValue * 40; 20 } 21 ) 22); 23 24for await (const output of generator(2)) { 25 console.log(output); //=> 4, 6, 8, 60, 90, 120, 8, 12, 16, 100, 150, 200, 12, 18, 24 26}
Uses Promise.race to race the next yield and the map function, skips the map output and aborts the passed in AbortSignal if the next yield wins.
1import { pipe, raceMap } from 'create-async-generator'; 2 3async function* inputGenerator(value) { 4 yield value * 2; 5 await new promise((resolve) => settimeout(resolve, 100)); 6 yield value * 3; 7 await new promise((resolve) => settimeout(resolve, 50)); 8 yield value * 4; 9} 10 11const generator = pipe( 12 inputGenerator, 13 raceMap((value, i, _abortSignal, _originalValue) => { 14 await new promise((resolve) => settimeout(resolve, 60)); 15 yield value * 10; 16 }) 17); 18 19for await (const output of generator(2)) { 20 console.log(output); //=> 40, 80 21}
No vulnerabilities found.
No security vulnerabilities found.