1

If you want to publish a module that has sequenced IO, is it ever OK to write,

./sequenced_actions.js
module.exports = function * () {}

Thereby permitting something like,

co( function * {
  yield require('./sequenced_actions');
} )();
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • why not? you can publish whatever you want – vkurchatkin Apr 02 '14 at 19:42
  • And clearly there are a lot of things you can publish that are horrible ideas. – Evan Carroll Apr 02 '14 at 19:44
  • 1
    well, it's the matter of opinion as you can see. my opinion is: it's ok, but generator-based flow control is not mainstream (yet), so it's may be a bit early. You can wrap it with `co`, and export a function but still you will have to explain users that your package requires 0.10.x and a flag. – vkurchatkin Apr 02 '14 at 21:25
  • also, it should be `yield *`, even less people know that thing. – vkurchatkin Apr 02 '14 at 21:27
  • @vkurchatkin `co` takes care of that. if it gets a generatorFunction it does the right thing. A better question would be how Co reacts if you do it the ES6 way with `yield *` I imagine bad things would happen. – Evan Carroll Apr 02 '14 at 21:29
  • you are right, `co` has a lot of magic baked in. `yield * require('./sequenced_actions')();` also works, though. I think from `co`s perspective it's the same as have all the `yield`s inline, but might be faster. – vkurchatkin Apr 02 '14 at 21:39
  • completly forgot that I have already done that: https://github.com/vkurchatkin/generator-foreach – vkurchatkin Apr 02 '14 at 21:43

2 Answers2

1

if you want your modules to reach the largest audience possible, just write them in promises. hopefully node v0.12 will have native promises, so this will make things easier

Jonathan Ong
  • 19,927
  • 17
  • 79
  • 118
1

Yes, it's okay to do that.

Generator function is just an ordinary function under the hood. And since node.js allows an arbitrary value to be exports object of a module, you can export whatever you want there.

alex
  • 11,935
  • 3
  • 30
  • 42