JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 68285
  • Score
    100M100P100Q44906F
  • License MIT

Create a promise waiting for an event

Package Exports

  • event-to-promise

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (event-to-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

event-to-promise

Build Status Dependency Status devDependency Status

Create a promise waiting for an event

Install

Download manually or with package-manager.

npm

npm install --save event-to-promise

Example

var eventToPromise = require('event-to-promise');

function createServer(port) {
  var server = require('http').createServer();
  server.listen(port);

  // The server will be returned once it has started listening.
  //
  // If an error happened, it will be forwarded instead.
  return eventToPromise(server, 'listening').then(function () {
    return server;
  });
};

// Using plain promise.
createServer(80).then(function (server) {
  console.log('Server listening on http://localhost:80/');
}).catch(function (error) {
  console.error('Server could not start:', error);
});

// Even better using generators!
require('bluebird').coroutine(function *() {
  try {
    var server = yield createServer(80);
    console.log('Server listening on http://localhost:80/');
  } catch (error) {
    console.error('Server could not start:', error);
  }
})();

API

eventToPromise(emitter, eventName, [options])

emitter

Required Type: object

The event emitter you want to watch an event on.

eventName

Required Type: string

The name of the event you want to watch.

options

array

Type: boolean Default: undefined

This option controls how a listener's parameters are resolved by its promise.

If true, the parameters are resolved as an array. If false, the first parameter is resolved. If undefined (the default), an array is resolved if there's more than one parameter; otherwise, the first parameter is resolved.

error

Type: string Default: "error"

The name of the event which rejects the promise.

ignoreErrors

Type: boolean Default: false

Whether the error event should be ignored and not reject the promise.

Contributing

Contributions are very welcome, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet