Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/staltz/callbag-to-async-iterable
👜 Convert any pullable callbag source to an AsyncIterable
https://github.com/staltz/callbag-to-async-iterable
Last synced: 5 days ago
JSON representation
👜 Convert any pullable callbag source to an AsyncIterable
- Host: GitHub
- URL: https://github.com/staltz/callbag-to-async-iterable
- Owner: staltz
- License: mit
- Created: 2018-02-10T20:11:02.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-30T13:08:40.000Z (over 6 years ago)
- Last Synced: 2024-12-21T08:45:11.965Z (23 days ago)
- Language: JavaScript
- Homepage:
- Size: 6.84 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome-callbags - to-async-iterable
README
# callbag-to-async-iterable
Convert any pullable callbag source to an AsyncIterable (`async function*`).
**NOTE:** uses [async iterators](https://github.com/tc39/proposal-async-iteration) from ES2018, which are known to only work in Node.js v10, Firefox 57+, and Edge 16.
Copy file `ff-example.js` and paste it in Firefox's console to see a successful run.
`npm install callbag-to-async-iterable`
## Example
```js
const {pipe, filter, take} = require('callbag-basics');
const toAsyncIterable = require('callbag-to-async-iterable');function pullableAsyncSource(start, sink) {
if (start !== 0) return;
let i = 0;
sink(0, t => {
if (t === 1) {
setTimeout(() => { sink(1, i++) }, 1000);
}
});
}async function main() {
const nums = pipe(
pullableAsyncSource,
filter(i => i % 2),
take(5),
toAsyncIterable
);for await (let x of nums) {
console.log(x);
}
}main();
// 1
// 3
// 5
// 7
// 9
```