Coder Social home page Coder Social logo

fo's Introduction

Fo

什么是Fo

Fo是一个简单的generator执行器,用来自动执行generator。其中还添加了控制权转换,用来进行多个generator“并发”执行。

安装

git clone git@github.com:xwchris/fo.git

运行

浏览器环境:

// generator函数
function *foo() {
  yield 1;
  fo.delay(2000);
  yield 2;
}
// 运行
var fo = Fo().run(foo);

Node环境:

// 引入Fo
var Fo = require('./fo.js');
// generator函数
function *foo() {
  yield 1;
  fo.delay(2000);
  yield 2;
}
// 运行
var fo = Fo().run(foo);

怎么使用Fo

使用Fo需要Fo()函数进行初始化,Fo(p1, p2, ...)可以传入多个初始值,该值可以在运行的各个generator参数中拿到,拿到的是按照传入顺序,数组形式的参数。调用Fo()之后返回一个Fo实例对象,同时各个API也返回该实例对象因此可以进行链式调用。

包含的API

  • Fo(p1, p2, p3, ...),构造函数用来构造Fo实例,可同时传入多个初始参数
  • Fo.prototype.run(g1, g2, ...),用来运行多个generator,运行顺序按照round-robin方式来进行
  • Fo.prototype.delay(time),传入延迟时间来延迟执行,单位ms
  • Fo.prototype.transfer(),将控制权交给下一个要执行的generator,在generator中使用

例子

examples提供了两个运行的例子,第一个例子是浏览器中的一个打乒乓的小例子,用来说明控制转换,这是直接拿Getting Concurrent With ES6 Generators中的ping-pong例子来用了,把其中的ASQ换成了自己的,感兴趣的可以看下大神的文章。第二个例子是node环境中用来展示延迟的例子。

fo's People

Contributors

xwchris avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.