发电机没有产量

问题描述 投票:3回答:2
var A = {
  demo : function() * {
    /* Some logic here, but no yield is used */
  } 
}

什么是generator方法没有yield什么用途?你有没有用过这样的东西?用例是什么?

javascript node.js ecmascript-6
2个回答
4
投票

它就像一个空函数一样完全相同 - 有人想调用一个函数,但是你无事可做。

类似地,空生成器函数是一个创建不执行任何操作的生成器的函数。它确实代表空序列。然而,没有yield的生成器函数不一定是空的 - 它仍然可以做某事并且具有结果值,但是根本没有中间结果。


0
投票

以下代码每100毫秒在响应上打印'someValue',持续5秒。它不使用yield

const Koa = require('koa');
const through = require('through');


(new Koa()).use(function *(){
  const tr = through();
  setInterval(() => tr.write('someValue\n'), 100);
  setTimeout(tr.end, 5000);
  this.body = tr;
}).listen(3003, () => {});

访问:curl localhost:3003

© www.soinside.com 2019 - 2024. All rights reserved.