Closed
Description
openedon Feb 4, 2020
Simpler API with support for async/await through the use of an object iterator.
This is inspired by Go's channels.
It's intended that this:
- Flattens code
- Makes streams chainable with promises (which are essentially streams which complete immediately).
- Enables use of try/catch for error handling
Piping operators, creating "subjects" and applying a "subscribe" function can happen in userland through libraries.
Example Implementation
Codesandbox
Example usage:
const numbers = new Observable(next => {
const interval = setInterval(next, 1000)
return () => clearInterval(interval)
})
for await (const number of numbers) {
console.log(number)
if (number === 4) {
break
}
}
API
interface Observable<T> {
[Symbol.Iterator]: Promise<T>
}
type TeardownFunc = () => void
type SetupFunc<T> = (
next: (value: T) => void,
complete: () => void,
) => TeardownFunc
type ObservableConstructor<T> = (
setup: SetupFunc<T>
) => Observable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Metadata
Assignees
Labels
No labels