2013-08-17 21 views

回答

0

tried to port a very small subset并失败。问题的关键是节点光纤的Fiber.yield()停止执行光纤的调用堆栈,而生成器的yield仅停止立即函数。虽然您可能能够实现行为相似的系统(如Task.js),但似乎API兼容的实现是不可能的。

+0

您是否尝试使用'yield *'?它允许您嵌套生成器函数,以便您还可以生成嵌套的生成器函数。 –

+0

断开链接!请修复。 – nalply

+0

@nalply,修正。谢谢! – hurrymaplelad

3

您是否在寻找类似https://github.com/visionmedia/co的产品?

自述:

var co = require('co'); 

co(function *(){ 
    var a = yield get('http://google.com'); 
    var b = yield get('http://yahoo.com'); 
    var c = yield get('http://cloudup.com'); 
    console.log(a.status); 
    console.log(b.status); 
    console.log(c.status); 
})() 

co(function *(){ 
    var a = get('http://google.com'); 
    var b = get('http://yahoo.com'); 
    var c = get('http://cloudup.com'); 
    var res = yield [a, b, c]; 
    console.log(res); 
})() 

有一个名为KOA(http://koajs.com)一个新的网络架构,是基于此。

+0

好东西。感谢您的高举。我想知道是否可以在发生器之上构建与[fibrous](https://github.com/goodeggs/fibrous)语法兼容的东西。我相信不是。 – hurrymaplelad

1

我编写周围纤维称为包装wait.for:https://github.com/luciotato/waitfor

然后我编写相同的功能搭配发电机:https://github.com/luciotato/waitfor-ES6

您可以比较两种看到发电机如何能够取代节点 - 光纤,但语法不同。

的一个重要区别,这使得不可能有相同的API, 是ES6的发电机本体是一种特殊的语法来实现:function* 而节点纤维允许你使用任何js函数。