
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
async-events-jr
Advanced tools
This is a simple library to extend the standard events
module so that
it can cope with asynchronous event listeners. It requires ES6 Promises
and async
functions. The standard EventEmitter methods are all
supported, including 'once' and 'prepend' listeners.
AsyncEventEmitter.emit()
guarantees that all of the listeners will be
called in the correct order. If a listener returns a Promise (e.g.
because it is an async
function) then emit
will wait for the promise
to resolve before any further listeners are called. Any other type of
value returned from a listener will be ignored (so standard synchronous
listeners will still work properly).
The return value from emit()
is a Promise that will be resolved when
all the listeners have been called. If there were no listeners then it
will resolve to false
, otherwise it will resolve to true
. If any
listener throws or returns a Promise that rejects, the emit
Promise
will immediately be rejected with that value.
As with the standard EventEmitter
, emit
will reject if an event
called error
is emitted and there are no listeners for it.
Normally the listeners are executed in a serial manner, as described
above. They can however be executed in a parallel fashion by prefixing
the event type with =
.
Unlike Promise.all
-style execution, parallel emit
will not return
until all the promises that were started have been fulfilled or
rejected. All the listeners will be started in order; if any throw
a synchronous exception then no later listeners will be started.
When all listeners have completed, if any threw exceptions or
returned Promises that rejected, the Promise returned by emit
will be rejected as well, with the value of whatever exception or
rejection came first.
To clarify, if we have four listeners:
ok1 = () => new Promise(resolve => setTimeout(resolve, 100))
ok2 = () => new Promise(resolve => setTimeout(resolve, 100))
thrower = () => { throw new Error('throw') }
rejecter = () => Promise.reject(new Error('reject'))
and we emit an event with listeners [ok1, thrower, ok2]
,
ok1
will run to completion, ok2
will not be called at all,
and emit
will reject with Error('throw')
after ok1
has
completed.
If we emit an event with listeners [ok1, rejecter, ok2]
,
both ok1
and ok2
will run to completion, and emit
will
reject with Error('reject')
after both ok1
and ok2
have
completed.
const AsyncEventEmitter = require('async-events-jr')
class Foo extends AsyncEventEmitter {
...
}
const emitter = new Foo()
emitter.on('test', async function () { ... })
emitter.on('test', function () { ... })
await emitter.emit('test') // process listeners in series
await emitter.emit('=test') // process listeners in parallel
FAQs
Async events framework
The npm package async-events-jr receives a total of 1 weekly downloads. As such, async-events-jr popularity was classified as not popular.
We found that async-events-jr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.