0.3.129 • Published 2 days ago

asygen v0.3.129

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 days ago

Asygen

0-Deps, simple and fast async generator library for browser and NodeJS.

Supports ESM and CommonJS modules.

Build Status NPM version Downloads Coverage Status Maintainability

Installation

Npm installation

npm install asygen

Yarn installation

yarn add asygen

Utilities

Deferred

  • Represents a deferred operation.
  • Provides methods resolve and reject to control the wrapped promise.
  • Exposes properties promise and status to get the underlying promise and its current status.

Queue

  • A queue system for handling asynchronous tasks.
  • Offers methods push, pull, and done to manage tasks.

Generatorify

  • Convert a task into an asynchronous iterable.
  • The iterable can be used in for await...of loops to process values as they're produced.

Combine

  • Combine multiple asynchronous iterables into a single iterable.
  • The resulting iterable will yield values from all input iterables and complete when all of them are done.

Usage

Create deferred operation

import { defer, Status } from 'asygen';

const deferred = defer<number>();
console.log(deferred.status); // Status.PENDING

deferred.resolve(42);
deferred.promise.then((value) => {
  console.log(value); // 42
  console.log(deferred.status); // Status.RESOLVED
});

Create a deferred operation from events

import { defer } from 'asygen';

const result = defer();

console.log(result.status); // pending

task.once('data', error.resolve);
task.once('error', error.reject);
await result.promise;

console.log(result.status); // resolved or rejected

Task queue

import { createQueue } from 'asygen';

const queue = createQueue<number>();

queue.push(1);
queue.push(2);
queue.push(3);

queue.pull().promise.then((value) => console.log(value)); // 1
queue.pull().promise.then((value) => console.log(value)); // 2

Generatorify

import { generatorify } from 'asygen';

const task = async (callback) => {
  await callback('Hello');
  await callback('World');
  return 'Done!';
};

const iterable = generatorify(task);

(async () => {
  for await (const value of iterable) {
    console.log(value); // "Hello", then "World"
  }
})();

Convert events to asyncGenerator

import { once } from 'node:events';
import { generatorify, Task } from 'asygen';

// send data from the event until process exit
const task: Task = async (send) => {
  process.on('data', send);
  await once(process, 'exit');
};

for await (const data of generatorify(task)) {
  // handle data
}

Combine tasks

import { generatorify, combine } from 'asygen';

const task1 = async (callback) => {
  await callback('Task1 - Hello');
  await callback('Task1 - World');
};

const task2 = async (callback) => {
  await callback('Task2 - Foo');
  await callback('Task2 - Bar');
};

const iterable1 = generatorify(task1);
const iterable2 = generatorify(task2);

const combined = combine(iterable1, iterable2);

(async () => {
  for await (const value of combined) {
    console.log(value); // Logs values from both task1 and task2
  }
})();

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))) {
  // handle data
}
// First:    0 1 2 3 4 -
// Second:   0 . . . . 1
// Combined: 0 0 1 2 3 4 1

License

License Apache-2.0 Copyright (c) 2023-present Ivan Zakharchanka

0.3.129

2 days ago

0.3.128

3 days ago

0.3.127

4 days ago

0.3.126

8 days ago

0.3.125

9 days ago

0.3.124

11 days ago

0.3.123

16 days ago

0.3.122

17 days ago

0.3.121

18 days ago

0.3.120

21 days ago

0.3.119

22 days ago

0.3.118

23 days ago

0.3.116

25 days ago

0.3.117

24 days ago

0.3.115

27 days ago

0.3.114

1 month ago

0.3.113

1 month ago

0.3.112

1 month ago

0.3.111

1 month ago

0.3.110

1 month ago

0.3.109

1 month ago

0.3.108

1 month ago

0.3.107

1 month ago

0.3.106

2 months ago

0.3.105

2 months ago

0.3.104

2 months ago

0.3.103

2 months ago

0.3.102

2 months ago

0.3.101

2 months ago

0.3.100

2 months ago

0.3.99

2 months ago

0.3.98

2 months ago

0.3.97

2 months ago

0.3.96

2 months ago

0.3.95

2 months ago

0.3.94

3 months ago

0.3.93

3 months ago

0.3.92

3 months ago

0.3.91

3 months ago

0.3.90

3 months ago

0.3.89

3 months ago

0.3.88

3 months ago

0.3.87

3 months ago

0.3.86

3 months ago

0.3.85

3 months ago

0.3.84

3 months ago

0.3.83

3 months ago

0.3.82

3 months ago

0.3.81

3 months ago

0.3.80

3 months ago

0.3.79

3 months ago

0.3.78

3 months ago

0.3.75

3 months ago

0.3.77

3 months ago

0.3.76

3 months ago

0.3.74

4 months ago

0.3.73

4 months ago

0.3.72

4 months ago

0.3.71

4 months ago

0.3.70

4 months ago

0.3.69

4 months ago

0.3.68

4 months ago

0.3.67

4 months ago

0.3.66

4 months ago

0.3.65

4 months ago

0.3.64

4 months ago

0.3.63

4 months ago

0.3.62

4 months ago

0.3.61

4 months ago

0.3.60

4 months ago

0.3.59

4 months ago

0.3.58

4 months ago

0.3.57

4 months ago

0.3.56

5 months ago

0.3.55

5 months ago

0.3.54

5 months ago

0.3.53

5 months ago

0.3.52

5 months ago

0.3.51

5 months ago

0.3.50

5 months ago

0.3.49

5 months ago

0.3.48

5 months ago

0.3.47

5 months ago

0.3.46

5 months ago

0.2.27

9 months ago

0.2.26

9 months ago

0.2.25

9 months ago

0.2.24

9 months ago

0.2.23

9 months ago

0.2.22

9 months ago

0.2.21

9 months ago

0.2.20

9 months ago

0.2.19

9 months ago

0.2.18

10 months ago

0.2.17

10 months ago

0.2.16

10 months ago

0.2.15

10 months ago

0.2.14

10 months ago

0.2.13

10 months ago

0.2.12

10 months ago

0.2.11

10 months ago

0.2.10

10 months ago

0.3.0

9 months ago

0.3.6

9 months ago

0.3.5

9 months ago

0.3.8

8 months ago

0.3.7

9 months ago

0.3.2

9 months ago

0.3.1

9 months ago

0.3.4

9 months ago

0.3.3

9 months ago

0.3.42

5 months ago

0.3.41

5 months ago

0.3.40

5 months ago

0.3.45

5 months ago

0.3.44

5 months ago

0.3.43

5 months ago

0.3.31

6 months ago

0.3.30

6 months ago

0.3.39

5 months ago

0.3.38

5 months ago

0.3.37

6 months ago

0.3.36

6 months ago

0.3.35

6 months ago

0.3.34

6 months ago

0.3.33

6 months ago

0.3.32

6 months ago

0.3.29

6 months ago

0.3.20

8 months ago

0.3.28

6 months ago

0.3.27

6 months ago

0.3.26

6 months ago

0.3.25

6 months ago

0.3.24

6 months ago

0.3.23

7 months ago

0.3.22

8 months ago

0.3.21

8 months ago

0.3.19

8 months ago

0.3.18

8 months ago

0.3.9

8 months ago

0.3.17

8 months ago

0.3.16

8 months ago

0.3.15

8 months ago

0.3.14

8 months ago

0.3.13

8 months ago

0.3.12

8 months ago

0.3.11

8 months ago

0.3.10

8 months ago

0.2.30

9 months ago

0.2.29

9 months ago

0.2.28

9 months ago

0.2.7

10 months ago

0.2.9

10 months ago

0.2.8

10 months ago

0.2.6

10 months ago

0.2.5

10 months ago

0.1.27

11 months ago

0.1.26

11 months ago

0.2.1

11 months ago

0.2.3

10 months ago

0.2.2

11 months ago

0.2.4

10 months ago

0.1.15

12 months ago

0.1.20

11 months ago

0.1.21

11 months ago

0.1.22

11 months ago

0.1.23

11 months ago

0.1.24

11 months ago

0.1.25

11 months ago

0.1.16

11 months ago

0.1.17

11 months ago

0.1.18

11 months ago

0.1.19

11 months ago

0.1.10

12 months ago

0.1.11

12 months ago

0.1.12

12 months ago

0.1.13

12 months ago

0.1.14

12 months ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.9

12 months ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.1.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago