最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Any way to exports a generator function? - Stack Overflow

programmeradmin5浏览0评论

An example

generator.js:

exports.read = function *(){
  var a = yield read('co.github.js');
  var b = yield read('co.recevier.js');
  var c = yield read('co.yield.js');
  console.log([a,b,c]);
}

function read(file) {
  return function(fn){
    fs.readFile(file, 'utf8', fn);
  }
}

co.js:

var co = require('co');
var fs = require('fs');
var gen = require('./generator')
/*function read(file) {
  return function(fn){
    fs.readFile(file, 'utf8', fn);
  }
}*/

co(gen.read)()

It seems that exports doesn't support generator function.

require, module, __filename, __dirname) { module.exports.read = function *(){
                                                                          ^
SyntaxError: Unexpected token *
at exports.runInThisContext (vm.js:69:16)
    at Module._pile (module.js:432:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:349:32)
    at Function.Module._load (module.js:305:12)
    at Function.Module.runMain (module.js:490:10)
    at startup (node.js:123:16)
    at node.js:1027:3

Why I want to do this? I just want to separate my data from Controllers. Any way to solve it?

An example

generator.js:

exports.read = function *(){
  var a = yield read('co.github.js');
  var b = yield read('co.recevier.js');
  var c = yield read('co.yield.js');
  console.log([a,b,c]);
}

function read(file) {
  return function(fn){
    fs.readFile(file, 'utf8', fn);
  }
}

co.js:

var co = require('co');
var fs = require('fs');
var gen = require('./generator')
/*function read(file) {
  return function(fn){
    fs.readFile(file, 'utf8', fn);
  }
}*/

co(gen.read)()

It seems that exports doesn't support generator function.

require, module, __filename, __dirname) { module.exports.read = function *(){
                                                                          ^
SyntaxError: Unexpected token *
at exports.runInThisContext (vm.js:69:16)
    at Module._pile (module.js:432:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:349:32)
    at Function.Module._load (module.js:305:12)
    at Function.Module.runMain (module.js:490:10)
    at startup (node.js:123:16)
    at node.js:1027:3

Why I want to do this? I just want to separate my data from Controllers. Any way to solve it?

Share Improve this question edited Jun 24, 2014 at 14:12 Tinple asked Jun 24, 2014 at 13:55 TinpleTinple 1,2711 gold badge11 silver badges11 bronze badges 5
  • Isn't that error saying something else? – putvande Commented Jun 24, 2014 at 13:59
  • Nope. It seems the require in node doesn't support generator. – Tinple Commented Jun 24, 2014 at 14:13
  • What's the * in the function definition? It's invalid javascript - which is what the error is plaining about – slebetman Commented Jun 24, 2014 at 14:18
  • It's generator function defined in ES6. V8 has supported it. – Tinple Commented Jun 24, 2014 at 14:19
  • Are you using --harmony flag? – esp Commented Jun 28, 2015 at 21:19
Add a ment  | 

3 Answers 3

Reset to default 6

You can use a variable to store it, and export it afterwards :

var myGenerator = function *() {
    // ...
}

module.exports = myGenerator;

In another file, then, you can require it :

var myGen = require('./myfirstfile.js');
// myGen is now myGenerator from above

you can export whatever you want, but please do not export generator functions in public modules. generators are control flow hacks. instead, return promises with co@4

exports.fn = co.wrap(function* () {
  return yield something() 
}

This could be the issue:

NodeJS console SyntaxError: Unexpected token * for generator

Furthermore, I would not export anything that is not an object, specially in ES6 having classes at your disposal.

发布评论

评论列表(0)

  1. 暂无评论