await-first
Wait the first event in a set of event emitters and event pairs, then clean up after itself.
Last updated 7 years ago by gxcsoccer .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install await-first 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.0.0                                ...           latest (7 years ago)

1 Versions

  • 1.0.0                                ...           7 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (6)

© 2010 - cnpmjs.org x YWFE | Home | YWFE