{"id":15441274,"url":"https://github.com/alanshaw/abortable-iterator","last_synced_at":"2025-10-17T07:02:22.334Z","repository":{"id":34646962,"uuid":"155538244","full_name":"alanshaw/abortable-iterator","owner":"alanshaw","description":"Make any iterator or iterable abortable via an AbortSignal","archived":false,"fork":false,"pushed_at":"2024-12-06T10:57:09.000Z","size":479,"stargazers_count":14,"open_issues_count":3,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T11:49:53.459Z","etag":null,"topics":["abort","abortable","abortcontroller","abortsignal","async","cancel","iterator","stop"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alanshaw.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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-10-31T10:22:10.000Z","updated_at":"2024-08-07T12:23:06.000Z","dependencies_parsed_at":"2024-06-18T15:22:35.102Z","dependency_job_id":"324e2377-34e8-4f05-8f0c-d635cb2c420e","html_url":"https://github.com/alanshaw/abortable-iterator","commit_stats":{"total_commits":62,"total_committers":5,"mean_commits":12.4,"dds":0.564516129032258,"last_synced_commit":"ad5f6c5079ef2a48f0750ac50f91acf5743b77df"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanshaw%2Fabortable-iterator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanshaw%2Fabortable-iterator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanshaw%2Fabortable-iterator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanshaw%2Fabortable-iterator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alanshaw","download_url":"https://codeload.github.com/alanshaw/abortable-iterator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247980808,"owners_count":21027803,"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":["abort","abortable","abortcontroller","abortsignal","async","cancel","iterator","stop"],"created_at":"2024-10-01T19:19:41.883Z","updated_at":"2025-10-17T07:02:17.301Z","avatar_url":"https://github.com/alanshaw.png","language":"TypeScript","readme":"# abortable-iterator\n\n[![codecov](https://img.shields.io/codecov/c/github/alanshaw/abortable-iterator.svg?style=flat-square)](https://codecov.io/gh/alanshaw/abortable-iterator)\n[![CI](https://img.shields.io/github/actions/workflow/status/alanshaw/abortable-iterator/js-test-and-release.yml?branch=master\\\u0026style=flat-square)](https://github.com/alanshaw/abortable-iterator/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)\n\n\u003e Make any iterator or iterable abortable via an AbortSignal\n\n# About\n\n\u003c!--\n\n!IMPORTANT!\n\nEverything in this README between \"# About\" and \"# Install\" is automatically\ngenerated and will be overwritten the next time the doc generator is run.\n\nTo make changes to this section, please update the @packageDocumentation section\nof src/index.js or src/index.ts\n\nTo experiment with formatting, please run \"npm run docs\" from the root of this\nrepo and examine the changes made.\n\n--\u003e\n\n## Example\n\n```js\nimport { abortableSource } from 'abortable-iterator'\n\nasync function main () {\n  // An example function that creates an async iterator that yields an increasing\n  // number every x milliseconds and NEVER ENDS!\n  const asyncCounter = async function * (start, delay) {\n    let i = start\n    while (true) {\n      yield new Promise(resolve =\u003e setTimeout(() =\u003e resolve(i++), delay))\n    }\n  }\n\n  // Create a counter that'll yield numbers from 0 upwards every second\n  const everySecond = asyncCounter(0, 1000)\n\n  // Make everySecond abortable!\n  const controller = new AbortController()\n  const abortableEverySecond = abortableSource(everySecond, controller.signal)\n\n  // Abort after 5 seconds\n  setTimeout(() =\u003e controller.abort(), 5000)\n\n  try {\n    // Start the iteration, which will throw after 5 seconds when it is aborted\n    for await (const n of abortableEverySecond) {\n      console.log(n)\n    }\n  } catch (err) {\n    if (err.code === 'ERR_ABORTED') {\n      // Expected - all ok :D\n    } else {\n      throw err\n    }\n  }\n}\n\nmain()\n```\n\n# Install\n\n```console\n$ npm i abortable-iterator\n```\n\n## Browser `\u003cscript\u003e` tag\n\nLoading this module through a script tag will make its exports available as `AbortableIterator` in the global namespace.\n\n```html\n\u003cscript src=\"https://unpkg.com/abortable-iterator/dist/index.min.js\"\u003e\u003c/script\u003e\n```\n\n## API\n\n```js\nimport {\n  abortableSource,\n  abortableSink,\n  abortableTransform,\n  abortableDuplex\n} from 'abortable-iterator'\n```\n\n- [`abortableSource(source, signal, [options])`](#abortablesource-signal-options)\n- [`abortableSink(sink, signal, [options])`](#abortablesinksink-signal-options)\n- [`abortableTransform(transform, signal, [options])`](#abortabletransformtransform-signal-options)\n- [`abortableDuplex(duplex, signal, [options])`](#abortableduplexduplex-signal-options)\n\n### `abortableSource(source, signal, [options])`\n\n**(alias for `abortable.source(source, signal, [options])`)**\n\nMake any iterator or iterable abortable via an `AbortSignal`.\n\n#### Parameters\n\n| Name                  | Type                                                                                                                                                                                                                                                 | Description                                                                                                                                                              |\n| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| source                | [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol)\\|[`Iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol) | The iterator or iterable object to make abortable                                                                                                                        |\n| signal                | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)                                                                                                                                                                        | Signal obtained from `AbortController.signal` which is used to abort the iterator.                                                                                       |\n| options               | `Object`                                                                                                                                                                                                                                             | (optional) options                                                                                                                                                       |\n| options.onAbort       | `Function`                                                                                                                                                                                                                                           | An (async) function called when the iterator is being aborted, before the abort error is thrown. Default `null`                                                          |\n| options.abortMessage  | `String`                                                                                                                                                                                                                                             | The message that the error will have if the iterator is aborted. Default \"The operation was aborted\"                                                                     |\n| options.abortCode     | `String`\\|`Number`                                                                                                                                                                                                                                   | The value assigned to the `code` property of the error that is thrown if the iterator is aborted. Default \"ABORT\\_ERR\"                                                   |\n| options.returnOnAbort | `Boolean`                                                                                                                                                                                                                                            | Instead of throwing the abort error, just return from iterating over the source stream.                                                                                  |\n| options.onReturnError | `Function`                                                                                                                                                                                                                                           | When a generator is aborted, we call `.return` on it - if this function errors the error value will be passed to the `.onReturnError` callback if passed. Default `null` |\n\n#### Returns\n\n| Type                                                                                                                      | Description                                                                                                     |\n| ------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |\n| [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol) | An iterator that wraps the passed `source` parameter that makes it abortable via the passed `signal` parameter. |\n\nThe returned iterator will `throw` an `AbortError` when it is aborted that has a `type` with the value `aborted` and `code` property with the value `ABORT_ERR` by default.\n\n### `abortableSink(sink, signal, [options])`\n\nThe same as [`abortable.source`](#abortablesource-signal-options) except this makes the passed [`sink`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#sink-it) abortable. Returns a new sink that wraps the passed `sink` and makes it abortable via the passed `signal` parameter.\n\n### `abortableTransform(transform, signal, [options])`\n\nThe same as [`abortable.source`](#abortablesource-signal-options) except this makes the passed [`transform`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) abortable. Returns a new transform that wraps the passed `transform` and makes it abortable via the passed `signal` parameter.\n\n### `abortableDuplex(duplex, signal, [options])`\n\nThe same as [`abortable.source`](#abortablesource-signal-options) except this makes the passed [`duplex`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) abortable. Returns a new duplex that wraps the passed `duplex` and makes it abortable via the passed `signal` parameter.\n\nNote that this will abort *both* sides of the duplex. Use `duplex.sink = abortable.sink(duplex.sink)` or `duplex.source = abortable.source(duplex.source)` to abort just the sink or the source.\n\n## Related\n\n- [`it-pipe`](https://www.npmjs.com/package/it-pipe) Utility to \"pipe\" async iterables together\n\n# API Docs\n\n- \u003chttps://alanshaw.github.io/abortable-iterator\u003e\n\n# License\n\nLicensed under either of\n\n- Apache 2.0, ([LICENSE-APACHE](https://github.com/alanshaw/abortable-iterator/LICENSE-APACHE) / \u003chttp://www.apache.org/licenses/LICENSE-2.0\u003e)\n- MIT ([LICENSE-MIT](https://github.com/alanshaw/abortable-iterator/LICENSE-MIT) / \u003chttp://opensource.org/licenses/MIT\u003e)\n\n# Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falanshaw%2Fabortable-iterator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falanshaw%2Fabortable-iterator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falanshaw%2Fabortable-iterator/lists"}