event-to-promise
Advanced tools
Comparing version 0.6.0 to 0.7.0
43
index.js
@@ -5,6 +5,4 @@ 'use strict' | ||
var AnyPromise = require('any-promise') | ||
function noop () {} | ||
// =================================================================== | ||
function makeEventAdder (emitter, arrayArg) { | ||
@@ -27,11 +25,13 @@ var addListener = | ||
var cleanUp = removeListener && function () { | ||
for (var i = 0, n = eventsAndListeners.length; i < n; i += 2) { | ||
removeListener.call(emitter, eventsAndListeners[i], eventsAndListeners[i + 1]) | ||
var cleanUp = removeListener | ||
? function () { | ||
for (var i = 0, n = eventsAndListeners.length; i < n; i += 2) { | ||
removeListener.call(emitter, eventsAndListeners[i], eventsAndListeners[i + 1]) | ||
} | ||
} | ||
} | ||
: noop | ||
return function (event, cb) { | ||
function addEvent (event, cb) { | ||
function listener () { | ||
cleanUp && cleanUp() | ||
cleanUp() | ||
@@ -56,2 +56,5 @@ var arg | ||
} | ||
addEvent.cleanUp = cleanUp | ||
return addEvent | ||
} | ||
@@ -62,4 +65,9 @@ | ||
function eventToPromise (emitter, event, opts) { | ||
return new AnyPromise(function (resolve, reject) { | ||
var cancel | ||
var promise = new Promise(function (resolve, reject) { | ||
var addEvent = makeEventAdder(emitter, opts && opts.array) | ||
cancel = function () { | ||
cancel = noop | ||
addEvent.cleanUp() | ||
} | ||
@@ -72,10 +80,18 @@ addEvent(event, resolve) | ||
}) | ||
promise.cancel = function () { return cancel() } | ||
return promise | ||
} | ||
var defaultErrorEvents = [ 'error' ] | ||
eventToPromise.multi = function eventsToPromise (emitter, successEvents, errorEvents, opts) { | ||
eventToPromise.multi = function eventsToPromise (emitter, successEvents, errorEvents) { | ||
errorEvents || (errorEvents = defaultErrorEvents) | ||
return new AnyPromise(function (resolve, reject) { | ||
var cancel | ||
var promise = new Promise(function (resolve, reject) { | ||
var addEvent = makeEventAdder(emitter, true) | ||
cancel = function () { | ||
cancel = noop | ||
addEvent.cleanUp() | ||
} | ||
@@ -90,4 +106,7 @@ var i, n | ||
}) | ||
promise.cancel = function () { return cancel() } | ||
return promise | ||
} | ||
module.exports = eventToPromise |
{ | ||
"name": "event-to-promise", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"license": "MIT", | ||
@@ -21,11 +21,11 @@ "description": "Create a promise waiting for an event", | ||
}, | ||
"dependencies": { | ||
"any-promise": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.2.5", | ||
"must": "^0.13.1", | ||
"native-promise-only": "^0.8.1" | ||
"native-promise-only": "^0.8.1", | ||
"standard": "^6.0.4" | ||
}, | ||
"scripts": { | ||
"lint": "standard", | ||
"posttest": "npm run lint", | ||
"test": "mocha index.spec.js" | ||
@@ -32,0 +32,0 @@ }, |
@@ -20,2 +20,6 @@ # event-to-promise | ||
This library requires promises support, for Node versions prior to 0.12 [see | ||
this page](https://github.com/julien-f/js-promise-toolbox#usage) to | ||
enable them. | ||
## Example | ||
@@ -74,3 +78,3 @@ | ||
### eventToPromise(emitter, event, [options]) | ||
### eventToPromise(emitter, event, [options]) => Promise | ||
@@ -80,2 +84,11 @@ > Wait for one event. The first parameter of the emitted event is used | ||
The returned promise has a `cancel()` method which can be used to | ||
remove the event listeners. Note that the promise will never settled | ||
if canceled. | ||
```js | ||
const promise = eventToPromise(emitter, 'foo') | ||
promise.cancel() | ||
``` | ||
#### emitter | ||
@@ -118,3 +131,3 @@ | ||
### eventToPromise.multi(emitter, successEvents, errorEvents) | ||
### eventToPromise.multi(emitter, successEvents, errorEvents) => Promise | ||
@@ -127,2 +140,11 @@ > Wait for one of multiple events. The array of all the parameters of | ||
The returned promise has a `cancel()` method which can be used to | ||
remove the event listeners. Note that the promise will never settled | ||
if canceled. | ||
```js | ||
const promise = eventToPromise(emitter, 'foo') | ||
promise.cancel() | ||
``` | ||
#### emitter | ||
@@ -129,0 +151,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8178
0
84
181
4
- Removedany-promise@^0.1.0
- Removedany-promise@0.1.0(transitive)