{"id":13725525,"url":"https://github.com/withspectrum/callback-to-async-iterator","last_synced_at":"2025-05-07T20:32:45.864Z","repository":{"id":57193082,"uuid":"124526665","full_name":"withspectrum/callback-to-async-iterator","owner":"withspectrum","description":"Turn any callback-based listener into an async iterator.","archived":true,"fork":false,"pushed_at":"2020-01-04T03:41:10.000Z","size":201,"stargazers_count":96,"open_issues_count":6,"forks_count":12,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-14T15:40:42.829Z","etag":null,"topics":["async","asynciterator","callbacks","iterall"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/withspectrum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-09T10:36:24.000Z","updated_at":"2024-06-28T11:53:17.000Z","dependencies_parsed_at":"2022-09-01T03:40:25.322Z","dependency_job_id":null,"html_url":"https://github.com/withspectrum/callback-to-async-iterator","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/withspectrum%2Fcallback-to-async-iterator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/withspectrum%2Fcallback-to-async-iterator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/withspectrum%2Fcallback-to-async-iterator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/withspectrum%2Fcallback-to-async-iterator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/withspectrum","download_url":"https://codeload.github.com/withspectrum/callback-to-async-iterator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252953716,"owners_count":21830890,"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","asynciterator","callbacks","iterall"],"created_at":"2024-08-03T01:02:26.040Z","updated_at":"2025-05-07T20:32:45.593Z","avatar_url":"https://github.com/withspectrum.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# `callback-to-async-iterator`\n\nTurn any callback-based listener into an async iterator.\n\nWe needed this module to turn our database listeners into async iterators, which is what GraphQL subscriptions expect to be passed. It might be useful for you too!\n\n```sh\nnpm install callback-to-async-iterator\n```\n\n## Usage\n\nImagine a standard callback-based listener like this:\n\n```JS \n// callback will be called with each new message added to the database\nconst listenToNewMessages = (callback) =\u003e {\n  return db.messages.listen(message =\u003e callback(message));\n}\n```\n\nThe problem is that callbacks are _push_ based, they push values to the listener whenever a new value is availabe. Async Iterators on the other hand are _pull_ based, they request a new value and wait until it is available.\n\nThis module reconciliates that difference so you can turn your standard callback-based listener into an async iterator:\n\n```JS\nimport asyncify from 'callback-to-async-iterator';\n\nconst messages = asyncify(listenToNewMessages);\n\n// Wait until the first message is sent\nconst firstMessage = await messages.next();\n\n// Asynchronously iterate over new messages and log them as they come in\nfor await (let message of messages) {\n  console.log(message);\n}\n\nconsole.log('Done!')\n```\n\nThis module will automatically buffer incoming data if `.next` hasn't been called yet.\n\n### Options\n\n- `onClose`: A function that's called with whatever you resolve from the listener after the async iterator is done, perfect to do cleanup\n- `onError`: A function that's called with any error that happens in the listener or async iterator\n- `buffering`: (default: `true`) Whether incoming values should be buffered in between requests for the next value in the async iterable (note: disabling this will make your iterator \"lossy\" if you don't immediately call `.next()`)\n\n\n```js\nasyncify(listenToNewMessages, {\n  // Close the database connection when the async iterator is done\n  // NOTE: This is passed whatever the listener resolves the returned promise with, in this case listenToNewMessages resolves with the database connection but it could be whatever you desire\n  onClose: (connection) =\u003e { connection.close(); },\n  // Log errors to your error tracking system\n  onError: (err) =\u003e {\n    errorTracking.capture(err);\n  },\n  buffering: false\n})\n```\n\n## Credits\n\nThis module is heavily based on the [event emitter to async iterator](https://github.com/apollographql/graphql-subscriptions/blob/master/src/event-emitter-to-async-iterator.ts) utility used in `graphql-js`. Also big shoutout to [@ForbesLindesay](https://github.com/ForbesLindesay) who helped a ton with the initial implementation and understanding the problem.\n\n## License\n\nLicensed under the MIT License, Copyright ©️ 2017 Maximilian Stoiber. See [LICENSE.md](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwithspectrum%2Fcallback-to-async-iterator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwithspectrum%2Fcallback-to-async-iterator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwithspectrum%2Fcallback-to-async-iterator/lists"}