Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thejohnfreeman/await-observable
Asynchronous functions for observables.
https://github.com/thejohnfreeman/await-observable
Last synced: 21 days ago
JSON representation
Asynchronous functions for observables.
- Host: GitHub
- URL: https://github.com/thejohnfreeman/await-observable
- Owner: thejohnfreeman
- License: mit
- Created: 2019-01-30T21:44:46.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-12T15:57:00.000Z (almost 6 years ago)
- Last Synced: 2024-03-23T09:29:50.078Z (10 months ago)
- Language: TypeScript
- Size: 13.7 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# await-observable
Asynchronous functions for observables.
[![npm](https://img.shields.io/npm/v/@thejohnfreeman/await-observable.svg)](https://www.npmjs.com/package/@thejohnfreeman/await-observable)
[![bundle size](https://img.shields.io/bundlephobia/minzip/@thejohnfreeman/await-observable.svg?style=flat)](https://bundlephobia.com/result?p=@thejohnfreeman/await-observable)
[![code style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat)](https://github.com/prettier/prettier)
[![build status](https://travis-ci.org/thejohnfreeman/await-observable.svg?branch=master)](https://travis-ci.org/thejohnfreeman/await-observable)A [longer version][] of the justification for this library is available on my
blog. In brief, we're missing the observable equivalent of asynchronous
functions. Chaining operators like `map`, `filter`, and `catchError` with
observables that yield a single value is as painful as chaining callbacks on
promises. Asynchronous functions let us write asynchronous code in
a synchronous style, which is easier to read, write, and comprehend. Short of
native syntax like `async`/`await`, we can offer a decorator for generator
functions.[longer version]: https://medium.com/@thejohnfreeman/escaping-pipeline-hell-38d962f66d31
## Usage
```typescript
import { async } from '@thejohnfreeman/await-observable'const logIn = async(function*({ username, password }) {
try {
const token = yield ajax.getJSON(
'https://example.com/login', { username, password })
return { value: token }
} catch (cause) {
return { error: 'Wrong username or password.' }
}
})const observable = logIn({ username, password }) // Request not yet sent.
const subscription = observable.subscribe( // Request sent now.
({ value }) => console.log('token', value),
({ error }) => console.error('error', error),
)
subscription.unsubscribe() // Request canceled.
```## FAQ
### Why did you name your export `async`?
To mimic asynchronous function syntax as closely as possible. If you don't
like that style, you can easily rename it:```typescript
import { async as myFavoriteName } from '@thejohnfreeman/await-observable'
```