events-to-any-promise
Create a promise waiting for events to be resolve/reject. Uses any-promise to create the promise
Last updated 5 months ago by franckldx .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install events-to-any-promise 
SYNC missed versions from official npm registry.

event-to-any-promise

Create a promise waiting for events to be resolve/reject. Uses any-promise to create the promise

Install

npm install --save events-to-any-promise

You must have any-promise 1.x in your dependencies.

Example

Using a promise to start Node Http Server:

'use strict';

const http = require('http');
const eventToAnyPromise = require('events-to-any-promise');

async function initHttpServer(config) {
  const server = http.createServer();
  server.listen(config);
  await eventToAnyPromise(server, 'listening');
  return server;
}


initHttpServer({ port: 80 })
  .then((server) => {
    console.log('Http server is listening on ', server.address());
  })
  .catch((err) => {
    console.log('Failed to start the server', err);
  });

Usage

Call events-to-any-promise to wait for a success event (this throws en error in case of error event):

const require('events-to-any-promise');
const someEventsEmitter = require('./someEventsEmitter');

async function doSomething() {
  await eventToAnyPromise(someEventsEmitter, 'sucess');
  // some stuff
}

When the success event returns a value:

const require('events-to-any-promise');
const someEventsEmitter = require('./someEventsEmitter');

async function doSomething() {
  const result = eventToAnyPromise(someEventsEmitter, 'sucess');
}

When the success event delivers multiples values, eventToAnyPromise returns an array:

const require('events-to-any-promise');
const someEventsEmitter = require('./someEventsEmitter');

async function doSomething() {
  const results = await eventToAnyPromise(someEventsEmitter, 'sucess');
  // results is an array
}

Be default, eventToAnyPromise uses 'error' as error event. To use another event as error event, simply pass it as 3rd parameters:

const require('events-to-any-promise');
const someEventsEmitter = require('./someEventsEmitter');

async function doSomething() {
  await eventToAnyPromise(someEventsEmitter, 'sucess', 'errorEvent');
  // some stuff
}

API

Returns a promise. The promise is resolve when sucessEvent is emitted and rejected when errorEvent is emitted.

eventToAnyPromise(emitter, successEvent, [errorEvent]) ==> Promise

emitter The events emitters object

successEvent The name of the success event

errorEvent The name of the error event. Optional, by default: 'error'.

Promise

The promise is build using any-promise, therefore it can be used with any Promise implementation.

Restrictions:

Requires Node 6.0.0 or upper.

License

MIT

Current Tags

  • 1.0.12                                ...           latest (5 months ago)

12 Versions

  • 1.0.12                                ...           5 months ago
  • 1.0.11                                ...           8 months ago
  • 1.0.10                                ...           8 months ago
  • 1.0.9                                ...           10 months ago
  • 1.0.8                                ...           a year ago
  • 1.0.7                                ...           a year ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (2)
Dev Dependencies (7)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |