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

await-first

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

await-first

Wait the first event in a set of event emitters and event pairs, then clean up after itself.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

await-first

Wait the first event in a set of event pairs, then clean up after itself.

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm install await-first --save

Example

  • awaitFirst(ee, events)
const EventEmitter = require('events');
const awaitFirst = require('await-first');

async function waitMessageOrClose(ee) {
  const o = await awaitFirst(ee, [ 'message', 'close' ]);

  switch (o.event) {
    case 'message':
      const msg = o.args[0]; // [ 'hello world' ]
      console.log('new message =>', msg);
      break;
    case 'close':
      console.log('closed');
      break;
  }
}

const ee = new EventEmitter();
waitMessageOrClose(ee);

setTimeout(() => {
  ee.emit('message', 'hello world');
}, 1000);
  • obj.awaitFirst(events)
const net = require('net');
const awaitFirst = require('await-first');

async function connect() {
  const socket = net.connect(8080, '127.0.0.1');
  socket.awaitFirst = awaitFirst;

  try {
    // wait `connect` or `error` event
    await socket.awaitFirst([ 'connect', 'error' ]);
  } catch (err) {
    console.log(err);
  }
  // ...
}

connect();

Keywords

FAQs

Package last updated on 23 Nov 2017

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