Asygen
0-Deps, simple and fast async generator library for browser and NodeJS.
Supports ESM and CommonJS modules.
Usage
Create deferred token
import { defer } from 'asygen';
const result = defer();
console.log(result.status);
task.once('data', error.resolve);
task.once('error', error.reject);
await result.promise;
console.log(result.status);
Convert events to asyncGenerator
import { once } from 'node:events';
import { generatorify, Task } from 'asygen';
const task: Task = async (send) => {
process.on('data', send);
await once(process, 'exit');
};
for await (const data of generatorify(task)) {
}
Combine generators
import { combine } from 'asygen';
const sleep = (timeout: number) => new Promise(resolve => setTimeout(resolve, timeout));
async function* generate(timeout: number, count: number) {
for (let index = 0; index < count; index++) {
yield index;
await sleep(timeout);
}
}
for await (const data of combine(generate(100, 5), generate(500, 2))) {
}
License
License Apache-2.0
Copyright (c) 2023-present Ivan Zakharchanka