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

invok

v1.0.2

Published

Another generator based flow control util

Downloads

20

Readme

invok

一个基于Promise+generator(yield)的异步流程控制库

##自述 好吧,这是一个轮子,功能上和TJ大神的co一模一样。
本来是我之前MVC框架内部使用的异步控制工具,现在重构一下单独剥离出来。
没有参考过co,思路基本上是一样的,但是我们的Promise风格完全不一样,他是基于es6的promise。
我是基于我的Promise A/+的一个实现(没错又是一个轮子=。=)
非要说有啥特别的地方?我自认为代码结构相对清晰易懂一些(只有70行),好吧,只是自认为而已。

对generator实现流程控制感兴趣的可以看下源代码。
另外自我打脸的是,浏览器运行版本为了不多引用promise库,用的也是es6风格promise(浏览器自带的promise)=。=
##安装

npm install invok

额不要吐槽我的名字,其实是就是invoke这个词的简写。
一来invoke普通单词也没啥意思,二来npm被别人抢注了,囧rz。

##测试用例+用法

var invok = require('invok');
var Promise = require('ipromise');
function delay(data, delay) { 
    //一个异步过程(返回promise)
    //这也代表了我的promise风格,这里把promise作为一种特殊的异步数据抽象。
    //所以它像普通数据一样经历创建、填充(异步的)和返回的过程
    //这样一来,同步函数返回同步数据,异步函数返回异步数据,写法上感觉一致化。而不是每次都把异步函数包装成一个promise(es6的风格)
    var p = new Promise();
    setTimeout(function() {
        p.resolve(data);
    }, delay || 500);
    return p;
}
/**
 * 用法跟co大致一样
 * invok接收三个参数 
 * 第一个参数可以是/函数/generator函数/generator/promise/普通数据
 * 当然 主要是用来运行generator函数的。
 * 第二个参数是运行第一个参数(如果是函数或者generator函数)时的this
 * 第三个参数是运行第一个参数(如果是函数或者generator函数)时的参数数组
 */ 
invok(function*() {
    //yield一个promise,返回这个promise的结果 
    console.log(yield delay("yield promise", 800));
    //yield 另外一个generator 返回这个generator的结果,(递归嵌套)
    var data1 = yield (function*() {
        return yield delay("yield a generator", 1500);
    })();
    console.log(data1);
    
    //yield一个promise的数组,首先这个数组里面的所有promise会并行执行,
    //等所有promise的执行完毕后,这个yield返回他们的结果组成的数组
    var data2 = yield [delay(1, 100), delay(2, 300), delay(3, 1000), delay(4, 500)];
    console.log(data2);
    
    //yield一个promise组成的对象,首先这个对象里面的所有promise会并行执行,
    //等所有promise的执行完毕后,这个yield返回他们的结果组成的对象,键和值还保持之前的对应关系
    var data3 = yield {e : delay(1, 100), b : delay(2, 300), c : delay(3, 1000), d : delay(4, 500)};
    console.log(data3);
    
    //yield普通对象,跟直接赋值没啥区别
    console.log(yield 123);
    console.log(yield {text : '普通对象'});
    return "invok end";
}).then(function(data) {
    //一次invok的执行结果
    console.log(data);
}, function(e) {
   //一次invok的执行结果
    console.log(e.stack)
});