🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

rx-from-async-iterator

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rx-from-async-iterator

Convert asyncGenerator object to Rx.Observable

Source
npmnpm
Version
1.4.0
Version published
Weekly downloads
26
18.18%
Maintainers
1
Weekly downloads
 
Created
Source

npm versionbuildcoverageinstall sizeMINIFIEDMINIFIED + GZIPPED

rx-from-async-iterator

A method to convert AsyncGeneratorObject to Rx.Observable

Install

npm i -S rx-from-async-iterator

Example

Simple usage

import { fromAsyncIterator } from 'rx-from-async-iterator';

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";
}

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

Real world example

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

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;
  }
});

FAQs

Package last updated on 21 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts