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

e-promise

v0.1.4

Published

在 Node 中,我们经常需要连接数据库、Redis、Mq、Socket、Rpc等等,它们都是基于回调通知是否连接成功,在连接成功的时候才能进行对应的操作,EventPromise 就是专门为了解决它而存在,让你使用同步的形式调用异步的回调。

Downloads

6

Readme

Build Status npm npm npm

EventPromise

安装

npm install e-promise
# or
yarn add e-promise

它可以做什么?

在 Node 中,我们经常需要连接数据库、Redis、Mq、Socket、Rpc等等,它们都是基于回调通知是否连接成功,在连接成功的时候才能进行对应的操作,EventPromise 就是专门为了解决它而存在,让你使用同步的形式调用异步的回调。

例子

// 这里用 socket.io 举个例子,其它的举一反三
const EventPromise = require('e-promise');
class IoEventPromise extends EventPromise {
	constructor() {
		super({
			// 设置超时时间,如果超过为异常
			timeout: 1000,
			// 设置 awaitPromise 最大的调用次数,如果超过为异常
			maxCall: 100
		});
		const server = require('http').createServer();
		const io = require('socket.io')(server);
		io.on('connection', client => {
			client.on('disconnect', () => {
				// 连接失败
				this.emitError('disconnect');
			});
			// 连接成功
			this.emitSuccess(client);
		});
		server.listen(3000);
	}
	async emit (eventName, ...args) {
		const client = await this.awaitPromise();
		// 连接失败,则直接输出错误
		if (!client || typeof client === 'string') {
			console.log(client);
			return;
		}
		// 连接成功,直接发送事件
		client.emit(eventName, ...args);
	}
}

const io = new IoEventPromise();
// 这里你就可以立即调用,发送事件,等 socket 连接成功后自行发送
io.emit('事件名称', '发送的参数');

options

  • options.timeout
    • 在异常的状态下,Promise超时时间,将会直接结束Promise,返回响应结果
    • default: 1000ms
  • options.maxCall
    • eventPromise.awaitPromise(); 在异常的状态下,超过最大的调用次数,将会直接结束Promise,返回响应结果
    • defalut: 100

API

  • eventPromise.emit(status: boolean, value: any = null);

    • status = true 的情况下,任何时候都会直接响应 Promise
    • status = false 的情况下,将会等待 options.timeout 或 options.maxCall 触发的时候结束 Promise
    • value Promise 的返回值
  • eventPromise.emitSuccess(value?: any);

    • 会立即响应 Promise 结果
  • eventPromise.emitError(value?: any);

    • 如果在 options.timeout 和 options.maxCall 指定的限制内,没有执行 eventPromise.emitSuccess() ,将会响应本结果
  • eventPromise.awaitPromise();

    • 等待 Promise 响应,并且可以拿到返回值