Regarding the send () javascript generator function

I looked at the generator functions on the Mozilla Dev page.

There was an example code that has a send () function .

function* fibonacci() {
  var a = yield 1;
  yield a * 2;
}

var it = fibonacci();
console.log(it);          // "Generator {  }"
console.log(it.next());   // 1
console.log(it.send(10)); // 20
console.log(it.close());  // undefined
console.log(it.next());   // throws StopIteration (as the generator is now closed)

But, both chrome and Firefox (latest version) throw an error on the send () function.

Any views on this? Is this not supported?

+4
source share
2 answers

.send Legacy, SpiderMonkey. . / ES6 ( 1215846, 1133277)

Firefox ( : 43.0.4). * , , yield, .

function fibonacci() {
  var a = yield 1;
  yield a * 2;
}

var it = fibonacci();
console.log(it);          
console.log(it.next());   
console.log(it.send(10)); 
console.log(it.close());  
console.log(it.next());
+2

, ESNext function.sent()

var result;
function* generator() {
  result = function.sent;
}
var iter = generator();
iter.next('tromple');
return result === 'tromple';

https://github.com/allenwb/ESideas/blob/master/Generator%20metaproperty.md

-1

Source: https://habr.com/ru/post/1625298/


All Articles