# rx-from-async-iterator

> Convert asyncGenerator object to Rx.Observable

Latest version **1.5.5** (published 2020-12-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install rx-from-async-iterator
pnpm add rx-from-async-iterator
yarn add rx-from-async-iterator
bun add rx-from-async-iterator
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.5 |
| Published | 2020-12-04 |
| First published | 2019-12-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 15.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | LPegasus |
| Maintainers | lpegasus |

## Links

- npm: https://www.npmjs.com/package/rx-from-async-iterator
- Repository: https://github.com/LPegasus/rx-from-async-iterator
- Homepage: https://github.com/LPegasus/rx-from-async-iterator#readme
- Issues: https://github.com/LPegasus/rx-from-async-iterator/issues
- npm.io page: https://npm.io/package/rx-from-async-iterator

## Dependencies (2)

- [rxjs](https://npm.io/package/rxjs.md) ^6.0.0
- [tslib](https://npm.io/package/tslib.md) ^1.10.0

## Recent versions

- 1.5.5 (latest) — 2020-12-04
- 1.5.4 — 2020-04-28
- 1.5.3 — 2020-04-27
- 1.5.1 — 2020-04-27
- 1.5.0 — 2020-03-27
- 1.4.0 — 2019-12-21
- 1.3.2 — 2019-12-16
- 1.3.1 — 2019-12-15
- 1.3.0 — 2019-12-15
- 1.2.0 — 2019-12-15
- 1.1.0 — 2019-12-15
- 1.0.0 — 2019-12-06
- 0.0.1 — 2019-12-06

## README

[![npm version](https://badge.fury.io/js/rx-from-async-iterator.svg)](https://www.npmjs.com/package/rx-from-async-iterator)[![build](https://travis-ci.org/LPegasus/rx-from-async-iterator.svg?branch=master)](https://travis-ci.org/LPegasus/rx-from-async-iterator)[![coverage](https://img.shields.io/codecov/c/github/LPegasus/rx-from-async-iterator.svg?style=flat-square)](https://codecov.io/gh/LPegasus/rx-from-async-iterator)[![install size](https://packagephobia.now.sh/badge?p=rx-from-async-iterator)](https://packagephobia.now.sh/result?p=rx-from-async-iterator)[![MINIFIED](https://badgen.net/bundlephobia/min/rx-from-async-iterator)](https://bundlephobia.com/result?p=rx-from-async-iterator)[![MINIFIED + GZIPPED](https://badgen.net/bundlephobia/minzip/rx-from-async-iterator)](https://badgen.net/bundlephobia/min/rx-from-async-iterator)

# rx-from-async-iterator

A method to convert **AsyncGeneratorObject** to [Rx.Observable](https://rxjs.dev/guide/observable)



## Install

`npm i -S rx-from-async-iterator`



## Example

### Simple usage

```typescript
import { fromAsyncIterator } from 'rx-from-async-iterator';
import { fromEvent } from 'rxjs';
import { take } from 'rxjs/operators';

const click$ = fromEvent('click', document.body).pipe(take(2));

async function sleepOneSecond() {
  return new Promise(r => setTimeout(r, 1000));
}

async function* subTask() {
  await sleepOneSecond();
  yield "d";
  await sleepOneSecond();
  yield "e";
  await sleepOneSecond();
}

async function* taskAsync(error?: Error) {
  yield "a";
  await sleepOneSecond();
  yield "b";
  await sleepOneSecond();
  yield "c";
  yield* subTask();
  yield "f";
  yield click$;
}

fromAsyncIterator(taskAsync()).subscribe(console.log);
/// output as follows:
// -> 'a'
// -> 'b'
// -> 'c'
// -> 'd'
// -> 'e'
// -> 'f'  /// and one second duration between each letter.
// -> MouseEvent
// -> MouseEvent
```



### Real world example

Imagine we have an QRCode sign up feature to do, we need to poll an backend interface called `/check/login`

```typescript
async function* pollCheckLogin() {
  let currentStatus = 'currentStatus';
	while(currentStatus === 'pending') {
    yield currentStatus = (await fetch('/check/login').then(resp => resp.json)).status;
  }
  if (currentStatus === 'timeout') {
		yield 'timeout';
  }
  if (currentStatus === 'success') {
    yield 'login success';
    return;
  }
	throw new Error('unknown status');
}

fromAsyncIterator(pollCheckLogin()).subscribe((status) => {
  switch(status) {
    case 'timeout':
      // handle timeout
      break;
    case 'login success':
      // go to home page
      break;
    case 'pending':
      // handle pending
      break;
  }
});
```

## [Try it](https://codepen.io/LPegasus/pen/ExgPQQz?editors=0011)

---
_Source: https://npm.io/package/rx-from-async-iterator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
