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

s94-node

v2.0.0

Published

常用的node工具方法封装---牧人与鱼

Downloads

14

Readme

s94-node

常用的一些工具函数

Install

$ npm install s94-node

Usage

基本用法

const $ = require('s94-node');
//传入字符串,等同require
const fs = $('fs'); 
//传入函数,创建回调函数队列对象,等同于new $.Read();
$(function(next){
	console.log('开始');
	setTimeout(()=>{next('字符串')},1000)
}).then(function(next, data){
	console.log(str);
})

Ready([f]).then(f)

回调函数队列对象,主要用于大量异步操作、动画等操作

  • f Function 执行的回调函数,也可以在then方法里面传入
    • 回调函数接收两个参数
    • @param {*} next 调用队列中的下一个函数,可以传一个参数
    • @param {*} data 上一个函数return的数据(不为undefined);或者上一个函数next方法执行传入的参数(只能传一个)
  • 返回: Ready 回调函数队列对象
    • Ready.then(f) f Function 添加回调函数到队列里面
//标准写法
var r = new $.Read(function(){})
//便捷写法
var r = $.Read(function(){});
var r = $(function(){});
//三种写法效果一样

//-----------用法----------
//写一个爬虫
http.request(url1, (res)=>{
	res.on('end',() => {
		//分析url1中的内容,获取指定地址url2
		http.request(url2, (res)=>{
			res.on('end',() => {
				//分析url2中的内容,获取需要的地址url3
				http.request(url3, (res)=>{
					res.on('end',() => {
						//分析url3中的内容,获取需要的地址url4
						//.......
					})
				})
			})
		})
	})
})
//用回调函数队列对象
$(function(next){
	http.request(url1, (res)=>{
		//分析url1中的内容,获取指定地址url2
		next(url2);
	})
}).then(function(next, url2){
	http.request(url2, (res)=>{
		//分析url2中的内容,获取指定地址url3
		next(url3);
	})
}).then(function(next, url3){
	http.request(url3, (res)=>{
		//分析url3中的内容,获取指定地址url4
		next(url4);
	})
}).then(function(next, url4){
	//......
})

关于和Promise的区别

1、Promise主要是构建一个更方便的异步执行的方式;Ready主要是建立一个函数执行队列。

Promise对不同异步执行的结果进行处理,拥有fulfilled和fulfilled两个状态,传入两个方法(resolve, reject)成功和失败分别调用;Ready只有一个方法(next)表示执行下一个函数;所以Ready主要用于多个操作队列的情况下

2、Promise的then方法返回的并不是原来的Promise对象;Ready的then方法返回的并不是原来的Ready对象。如下

var r = $(function(next){
	next();
})
var r2 = r.then(function(next,data){})
console.log(r2===r); //true

var r = new Promise(function(ok, fail){
	ok();
})
var r2 = r.then(function(data){})
console.log(r2===r); //false
//所以下面三种写法,写法2和其余两种不同
//写法1
var r = new Promise(function(ok, fail){
	ok();
}).then().then();
//写法2
var r = new Promise(function(ok, fail){
	ok();
});
r.then();
r.then();
//写法3
var r = new Promise(function(ok, fail){
	ok();
})
r = r.then();
r = r.then();