npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

evt-promise

v1.1.0

Published

An event-driven promise-like function wrapper

Downloads

5

Readme

evt-promise

Build Status Build Status Coverage Status

An event-driven promise-like function wrapper, has a different api through the ES6 Promise

Install

npm install evt-promise --save

Usage

If you don’t understand the examples below or you want to see more detailed usage, have a look at this

BASIC - Legacy API:

const evt = require('evt-promise');

//Simple usage
var foo = evt(function() {return true;});
foo.exec(function(value){console.log(value);},function(e) {throw e;}); //Will print true

//Catch errors
var bar = evt(function() {throw new Error('TEST');});
bar.exec(function(v){}, function(e){console.error(e.message);}); //Will print 'TEST' at stderr

//Pass arguments
var baz = evt(function(arg) {return arg;},[true]);
baz.exec(function(value){console.log(value);},function(e) {throw e;}); //Will print true

BASIC - new Promise-like API

const evt = require('evt-promise');

//Simple usage
var foo = evt.promiseLike(function() {this.emit('resolved',true)});
foo.then(function(value){console.log(value);})
   .catch(function(e) {throw e;}); //Will print true

//Catch errors
var bar = evt.promiseLike(function() {this.emit.('rejected', new Error('TEST'));});
bar.then(function(v){})
   .catch(function(e){console.error(e.message);}); //Will print 'TEST' at stderr

//Pass arguments
var baz = evt.promiseLike(function(arg) {this.emit('resolved',arg)},[true]);
baz.then(function(value){console.log(value);})
   .catch(function(e) {throw e;}); //Will print true

Warning: If you are using arrow function as the task in any of the examples that uses new Promise-like API, they won't work because you can't inject a this object to an arrow function

Advanced - Change the task that you want to run, Change the arguments that you want to pass to the task function - Legacy API:

const evt = require('evt-promise');

//Change the task
var foo = evt(function() {return true});
foo.task = function() {
	return false;
}
foo.exec(function(value){console.log(value);},function(e) {throw e;}); //Will print false, not true

//Change the arguments
var bar = evt(function(arg) {return arg;},[true]);
bar.args = [false];
bar.exec(function(value){console.log(value);},function(e) {throw e;}); //Will print false not true

Advanced - Emit custom events in the task and handle it - Legacy API

const evt = require('evt-promise');

var fn = function() {
	this.emit('custom','custom event'); //The this object is an instance of eventemitter2, injected by the apply() function
	return 'hello custom';
};

var resHandler = function(value) {
	console.log(value);
};

var rejHandler = function(e) {
	throw e;
};

var foo = evt(fn)

foo.event.on('custom', function(value) {console.log(value);});

foo.exec(resHandler,rejHandler); //Will print 'custom event' and then 'hello coustom'

Warning: If you use arrow function as the task, this example won't work because you cannot inject a this object to an arrow function