What does `return function * () {...}` mean mean?

I saw something strange in Koa . It has several new function names (from https://github.com/koajs/koa/blob/master/examples/co.js#L10 ):

app.use(function(){ return function *(){ var paths = yield fs.readdir('docs'); var files = yield paths.map(function(path){ return fs.readFile('docs/' + path, 'utf8'); }); this.type = 'markdown'; this.body = files.join(''); } }); 

What does return function *() mean? Can we declare a function named * in JavaScript?

+58
javascript ecmascript-6
Nov 07 '13 at
source share
2 answers

This means that the function returns an iterator (so it can be called multiple times with .next () to get more values.

browse http://wingolog.org/archives/2013/05/08/generators-in-v8 for more information

This is an ES6 construct, so at the moment you see it more in node and not on client side js

+34
Nov 07 '13 at 10:52
source share

Koa uses a new JavaScript feature called generators, and * is a way to identify the generator in V8.

+18
Nov 07 '13 at
source share



All Articles