Socket
Book a DemoInstallSign in
Socket

async-subscribe

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-subscribe

Emit events when you call an asynchronous function

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
2
-77.78%
Maintainers
1
Weekly downloads
 
Created
Source

async subscribe

Emit lifecycle events from an asynchronous function call.

install

npm install async-subscribe

example

var subscribe = require('../');
var EE = require('events').EventEmitter;
var emitter = new EE();
var assert = require('assert');

emitter.on('myEvent', function (resp) {
    console.log('emits api event', resp);
});

emitter.on('asyncStart', function (args) {
    console.log('emits async start event');
    assert.deepEqual(args, { args: 'example' }, 'passes args to listener');
});

emitter.on('error', function (err) {
    console.log('emits error event', err);
});

function callAPI (opts, cb) {
    assert.deepEqual(opts, { args: 'example' },
        'should pass along args'
    );
    process.nextTick(cb.bind(null, null, { test: 'response' }));
}

function myCallback (err, resp) {
    console.log('calls my callback', err, resp);
}

var callAndEmit = subscribe(emitter, 'myEvent', callAPI);

// call this the same way as the original function, but now we emit
// events too
callAndEmit({ args: 'example' }, myCallback);

Keywords

async

FAQs

Package last updated on 21 Sep 2016

Did you know?

Socket

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.

Install

Related posts