Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@async-generators/from-emitter

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@async-generators/from-emitter

convert an event-emitter to an async-iterable

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
252
increased by27.27%
Maintainers
1
Weekly downloads
 
Created
Source

from-emitter

logo

inform an iterable when it is prematurely terminated by the consumer.

NPM version Travis Status Travis Status

Install

yarn add @async-generators/from-emitter

This package's main entry points to a commonjs dist. The module entry points to a es2015 module dist. Both require native async-generator support, or be down-compiled with a webpack loader.

Exports

(default) from-emitter(emitter, onNext, onError, onDone [,selectNext][,selectError])

from-emitter() subscribes to onNext, onError and onDone and returns a (one-time) iterable-sequence of captured events. When the event listeners are called, the arguments are passed to selectNext(...args) and selectError(...args) to pick a value (defaults to the first argument). If the sequence completes (onDone), or the consumer terminates early, the event listeners are detached from the emitter and the iterable becomes disposed and cannot be iterated again.

source must be a node-js compliment event emitter, with the addListener and removeListener methods.

Example

example.js

const fromEmitter = require('./dist/commonjs').default;
const {EventEmitter} = require('events');

async function main() {
  let events = new EventEmitter();
  let source = fromEmitter(events, "data", "error", "close");
  let consumer = new Promise(async done => {
    for await (let item of source) {
      console.log(item);
    }
    console.log("...and we're done!")
    done();
  });

  events.emit("data", 1);
  events.emit("data", 2);
  events.emit("data", 3);
  events.emit("data", 4);
  events.emit("close");

  await consumer;
}

main().catch(console.log);

Execute with the latest node.js:

node example.js // or...
node --harmony-async-iteration example.js 

output:

1
2
3
4
...and we're done!

Typescript

This library is fully typed and can be imported using:

import fromEmitter from '@async-generators/from-emitter');

It is also possible to directly execute your properly configured typescript with ts-node:

ts-node --harmony_async_iteration example.ts

Keywords

FAQs

Package last updated on 16 Jan 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc