{"id":23640957,"url":"https://github.com/pedromsilvapt/data-async-iterator","last_synced_at":"2025-08-31T17:32:14.081Z","repository":{"id":142836206,"uuid":"135748248","full_name":"pedromsilvapt/data-async-iterator","owner":"pedromsilvapt","description":"Batteries-included utility functions to work with async iterables as available in ES2018/TypeScript","archived":false,"fork":false,"pushed_at":"2022-04-24T20:35:35.000Z","size":112,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-09T00:54:42.806Z","etag":null,"topics":["async","es2018","generators","iterables","iterators","nodejs","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pedromsilvapt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-01T17:57:07.000Z","updated_at":"2023-06-06T23:44:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"fefef418-7d1c-4951-bd14-145edd14089a","html_url":"https://github.com/pedromsilvapt/data-async-iterator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedromsilvapt%2Fdata-async-iterator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedromsilvapt%2Fdata-async-iterator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedromsilvapt%2Fdata-async-iterator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedromsilvapt%2Fdata-async-iterator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pedromsilvapt","download_url":"https://codeload.github.com/pedromsilvapt/data-async-iterator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231610637,"owners_count":18400207,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","es2018","generators","iterables","iterators","nodejs","typescript"],"created_at":"2024-12-28T09:53:35.857Z","updated_at":"2024-12-28T09:53:36.489Z","avatar_url":"https://github.com/pedromsilvapt.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Async Iterator\r\n\r\n\u003e Batteries-included utility functions to work with async iterables as available in ES2018/TypeScript\r\n\r\n# Installation\r\n```shell\r\nnpm install --save data-async-iterators\r\n```\r\n\r\n# Tips \u0026 Tricks\r\n - Iterators as lazy/pull-based\r\n    - They only calculate the next value when it is requested; thus only calculating the values that are needed\r\n - Some methods require buffering values: be careful when mixing them with slow consumers\r\n - Iterators need consumers: since transformations are lazy, not consuming (subscribing) to an iterator means nothing happens\r\n - When manually using an iterator (calling `next()`), one should be careful to call `return()` on iterators that provide it as well, when the iterator is not needed anymore before it has ended, to allow it to free any resources it might be holding\r\n - Most operators return iterables. If provided with iterables as well, they can be iterated multiple times (instead of just once). Other iterators return iterators: these can only be iterated once\r\n - Most operators in this library accept `AsyncIterableLike\u003cT\u003e` instead of `AsyncIterable\u003cT\u003e`. This means certain rules apply:\r\n    - `Iterable\u003cT\u003e`'s are transformed to `AsyncIterable\u003cT\u003e`'s;\r\n    - `Iterator\u003cT\u003e`'s and `AsyncIetrator\u003cT\u003e`'s are transformed to `AsyncIterable\u003cT\u003e`'s that always return the same, original iterator;\r\n    - `Promise\u003cAsyncIterableLike\u003cT\u003e\u003e`'s are converted to `AsyncIterable\u003cT\u003e`, waiting for the promise before using the resolved iterable;\r\n    - The operator `fromPromise\u003cT\u003e( promise : Promise\u003cT\u003e )` returns an `AsyncIterable\u003cT\u003e` that only ever emits one value or one exception, whatever is resolved by the promise;\r\n\r\n# Usage\r\nContains all the common utility functions like map, filter, takeWhile, flatMap, concat, and many more as well as more async-centric ones\r\nlike flatMapConcurrent, debounce, throttle, buffered, etc...\r\n\r\n```typescript\r\nimport { from, delay, map, flatMapConcurrent } from 'data-async-iterators';\r\n\r\n// Create an asynchonous iterable stream\r\nconst source = delay( from( [ 1, 2, 3, 4 ] ), 1000 );\r\n\r\n// A closure that takes a number and slowly returns the number and it's square\r\nconst mapper = number =\u003e delay( from( [ number, number * number ] ), 4000 );\r\n\r\n// Run mapper concurrently only twice\r\nconst flatMapConcurrent( source, mapper, 2 );\r\n\r\n// And finally consume the values (returns a promise notifying when the iterator ends)\r\nforEach( source, res =\u003e console.log( res ) );\r\n```\r\n\r\nOr maybe a more pratical example\r\n```typescript\r\nimport { merge, map, forEach } from 'data-async-iterators';\r\n\r\nfunction findDevices () : AsyncIterable\u003cDevice\u003e { /* ... */ };\r\n\r\nfunction connectDevice ( device : Device ) : AsyncIterable\u003cDeviceStatus\u003e { /* ... */ };\r\n\r\nfunction processStatus ( status : DeviceStatus ) : Promise\u003cvoid\u003e { /* ... */ };\r\n\r\n// Gets an async iterable of devices found\r\nconst devices : AsyncIterable\u003cDevice\u003e = findDevices();\r\n\r\n// For each iterable calls the connectDevice that returns an iterable documenting the statuses changes of each device\r\nconst statuses : AsyncIterable\u003cDeviceStatus\u003e = merge( map( devices, connectDevice ) );\r\n\r\n// Consumes all \r\nforEach( statuses, processStatus );\r\n```\r\n\r\nSometimes chaining functions in this way is not very readable, and therefore this package provides a utility class called `AsyncStream` that is a simple wraper around an iterable with all the operators as methods.\r\n\r\n```typescript\r\nimport { AsyncStream } from 'data-async-iterators';\r\n\r\nconst stream = AsyncStream.range( 1, 10 )\r\n    // Delay each number by 100 milliseconds\r\n    .delay( 100 )\r\n    // Double each number\r\n    .map( v =\u003e v * 2 )\r\n    // For each n number, generate n repetitions\r\n    .flatMap( v =\u003e AsyncStream.repeat( v, v ) )\r\n    // Ignore the first and last ten numbers\r\n    .slice( 10, -10 );\r\n\r\n// Since AsyncStream is a regular iterable, we can\r\nfor await ( let number of stream ) {\r\n    console.log( number );\r\n}\r\n\r\n// Or\r\nstream.forEach( number =\u003e console.log( number ) );\r\n\r\n// To convert any regular AsyncIterable (or promises, regular iterables, arrays, etc...)\r\n// into an AsyncStream just do:\r\nconst stream = new AsyncStream( iterable );\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedromsilvapt%2Fdata-async-iterator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpedromsilvapt%2Fdata-async-iterator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedromsilvapt%2Fdata-async-iterator/lists"}