Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/staltz/callbag-to-awaitable
👜 Use async-await to consume a pullable callbag source
https://github.com/staltz/callbag-to-awaitable
Last synced: 5 days ago
JSON representation
👜 Use async-await to consume a pullable callbag source
- Host: GitHub
- URL: https://github.com/staltz/callbag-to-awaitable
- Owner: staltz
- License: mit
- Created: 2018-02-15T12:45:58.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-16T17:43:08.000Z (almost 7 years ago)
- Last Synced: 2024-12-24T20:38:16.975Z (20 days ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 16
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.js
- License: LICENSE
Awesome Lists containing this project
- awesome-callbags - to-awaitable
README
/**
* callbag-to-awaitable
* --------------------
*
* Use the `async`/`await` syntax with pullable Callbags! There are actually no
* Promises in this package, just async-await syntax on objects with a `then`
* method.
*
* `npm install callbag-to-awaitable`
*
* Example: on an async pullable callbag source, apply some operators, then
* convert to an awaitable and consume it in an `async`/`await` function:
*
* const {pipe, take, map} = require('callbag-basics');
* const toAwaitable = require('callbag-to-awaitable');
*
* function pullableAsyncSource(start, sink) {
* if (start !== 0) return;
* let i = 0;
* sink(0, t => {
* if (t === 1) {
* setTimeout(() => { sink(1, i++) }, 1000);
* }
* });
* }
*
* const source = pipe(
* pullableAsyncSource, // 0, 1, 2, 3, 4, 5, 6, 7...
* take(5), // 0, 1, 2, 3, 4
* map(x => x / 4) // 0, 0.25, 0.5, 0.75, 1
* );
*
* async function main() {
* const next = toAwaitable(source);
* try {
* while (true) {
* const x = await next;
* console.log(x);
* }
* } catch (end) {
* console.log('done');
* }
* }
*
* main()
* // 0
* // 0.25
* // 0.5
* // 0.75
* // 1
* // done
*
* For more inquiries, read or fork the source below:
*/function toAwaitable(source) {
let talkback;
let resolve;
let reject;
let reason = false;
source(0, (t, d) => {
if (t === 0) talkback = d;
if (t === 1 && resolve) resolve(d);
if (t === 2) {
reason = d || true;
if (reject) reject(reason);
}
resolve = void 0;
reject = void 0;
});
return {
then: (_resolve, _reject) => {
if (reason) _reject(reason);
resolve = _resolve;
reject = _reject;
if (talkback) talkback(1);
},
};
}module.exports = toAwaitable;