Socket
Book a DemoInstallSign in
Socket

async-event

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-event

Simple event for async tasks

1.0.0
latest
npmnpm
Version published
Maintainers
1
Created
Source

async-event

Simple async event handling and interception.

Install

npm install --save async-event

Usage

Requires a promise implementation to be global

var AsyncEvent = require('async-event');

var user = { id: 123 };
var event = new AsyncEvent(user);

myPubSub.publish('my-event', event);

event
  .then(function() {
    console.log('Passed async validation!');
  })
  .catch(function() {
    console.log('Failed async validation :(');
  });

// Some other listener

myPubSub.subscribe('my-event', function(event) {
  event.intercept(function(user) {
    return fetch('/user/123/validate', {
      method: 'POST',
      body: JSON.stringify(user)
    });
  })
});

API

AsyncEvent(value, promise) ⇒ AsyncEvent

The constructor.

ParamTypeDescription
valueanyThe initial value
promisePromiseAn optional starting promise

intercept(handler) ⇒ void

A function that can perform a sync or async task. Handlers are executed in serial order.

ParamTypeDescription
handlerFunctionThe intercept function

getValue() ⇒ any

Gets the initial value the event initialized with.

then(fn) ⇒ Promise

Delegates to the internal Promise. Subscribes to the chain at that point in time. This will not add to the chain. This can be used to perform a task that you would want to happen in parallel.

ParamTypeDescription
fnFunctionThe promise function

catch(fn) ⇒ Promise

Same as then except the catch equivalent.

ParamTypeDescription
fnFunctionThe promise function

Keywords

async

FAQs

Package last updated on 22 Feb 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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.