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

lei-coroutine

v1.6.3

Published

Simple coroutine library, can keep function name and parameters information

Downloads

21

Readme

NPM version build status Test coverage David deps node version npm download npm license

lei-coroutine

简单的 coroutine 库,支持 TypeScript

为什么要造这个轮子

实际上已经有两个大家所熟知的库实现了类似的功能:cobluebird。 相比而言,这两个库的功能更强大,而且都久经考验。 但是我觉得它们也有各自的不足,以下是我造此轮子的理由:

  • async function / await是未来的主流,尽管现在的主流 JavaScript 引擎还没有支持。 但是我们要统一异步函数的格式:全部返回Promise
  • co支持的功能太过强大,可以yield的类型实在太多, 而有时候我们只想要一个可以简单替换async function / await语法的方式。 过多的功能会让人忍不住去使用,而未来替换的时候就更麻烦。
  • 此轮子实际上更多参考自babel_asyncToGenerator函数。 无论是bluebird还是co,在生成的包装函数中,都没有保留原函数的名字和参数列表等信息, 在调试的时候会造成一些困难。

特点

  • 保留原函数名字和参数列表信息
  • 返回的包装函数中包含了定义函数时的文件名及行号信息
  • 支持try catch捕捉错误,可以达到与async function一样的效果
  • 自带两个常用的函数delay(延迟,类似setTimeout())和parallel(并发执行,类似Promise.all()

安装

$ npm install lei-coroutine --save

使用方法

'use strict';

const coroutine = require('lei-coroutine');

// 包装 generator 函数
const fn = coroutine.wrap(function* (a) {
  // coroutine.delay() 是内置的一个等待函数,效果类似于 setTimeout
  yield coroutine.delay(a);
});
fn(100).then(ret => console.log(ret)).catch(err => console.error(err));

// 直接执行 generator 函数
coroutine(function* (a) {
  yield coroutine.delay(a);
}, 100).then(ret => console.log(ret)).catch(err => console.error(err));

// 并行执行多个任务的例子
coroutine(function* (a, b) {
  yield coroutine.parallel([
    coroutine.delay(a),
    coroutine.delay(b),
  ]);
}, 100, 120).then(ret => console.log(ret)).catch(err => console.error(err));

// 执行回调函数
coroutine.cb(null, function (a, b, callback) {
  callback(null, a + b);
}, 123, 456).then(ret => console.log(ret)).catch(err => console.error(err));

// 为了兼容 callback 方式,执行 generator 函数后可以 callback 方式回调
coroutine.asCallback(function* (a) {
  yield coroutine.delay(a);
}, 100, (err, ret) => {
  if (err) {
    console.error(err);
  } else {
    console.log(ret);
  }
});

自定义 Promise

如果想使用诸如bluebird这样的Promise库,可以通过设置lei-coroutine模块的Promise属性来完成:

const coroutine = require('lei-coroutine');
const bluebird = require('bluebird');

coroutine.Promise = bluebird;

与 async function / await 的替换

'use strict';

const coroutine = require('lei-coroutine');

// 声明函数
async function fn(a) {
  // coroutine.delay() 是内置的一个等待函数,效果类似于 setTimeout
  await coroutine.delay(a);
}
fn(100).then(ret => console.log(ret)).catch(err => console.error(err));

// 直接执行
(async function (a) {
  await coroutine.delay(a);
})(100).then(ret => console.log(ret)).catch(err => console.error(err));

// 并行执行多个任务的例子
(async function (a, b) {
  await coroutine.parallel([
    coroutine.delay(a),
    coroutine.delay(b),
  ]);
})(100, 120).then(ret => console.log(ret)).catch(err => console.error(err));

License

MIT License

Copyright (c) 2016 Zongmin Lei <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.