Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to exports a generator function?

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._compile (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?

like image 765
Tinple Avatar asked Jun 24 '14 13:06

Tinple


People also ask

Can a generator return a function value?

A return statement in a generator, when executed, will make the generator finish (i.e. the done property of the object returned by it will be set to true ). If a value is returned, it will be set as the value property of the object returned by the generator.

Can we export function?

export function function_name: This syntax is used to export the function in TypeScript. Yes, we can export the functions in TypeScript by using the 'export' keyword at the start of the function.

Which is the correct way to declare a generator function?

// Declare a generator function with a single return value function* generatorFunction() { return 'Hello, Generator!' } The Generator object returned by the function is an iterator. An iterator is an object that has a next() method available, which is used for iterating through a sequence of values.

Can we export function in node?

exports in Node. js is used to export any literal, function or object as a module. It is used to include JavaScript file into node. js applications.


2 Answers

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
like image 114
nha Avatar answered Sep 29 '22 02:09

nha


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() 
}
like image 33
Jonathan Ong Avatar answered Sep 29 '22 01:09

Jonathan Ong