I was looking at the mdn javascript reference and noticed that yield is listed in the operators section. On the other hand return is listed as a statement. I also found yield has a operator precedence of 2.
What features of yield make it fall into the operator class rather than a statement? Why does return fall into statements rather than operators?
2 Answers 2
It is an operator because it can be used in an expression.
function* g() {
value = 3;
while (value !== 5) value = Math.floor(yield value + 1);
}
var gen = g();
console.log(gen.next().value);
console.log(gen.next(1.5).value);
1 Comment
I am not certain on this, but in the context of a generator yield sends data to the generator.next() in that way it operates much like a function. Operators are special classes of functions in the most languages (JavaScript inlcuded).
You could almost imagine the generator.next calling into its instance passing a callback as to where to resume. And yield invoking that callback
Return signals the end of a path of execution and to replace a return value into the proper memory location and unwind the call stack by 1 unit. If feels primordial to the definition of the language,
awaitis listed as expression. But it look/work same as yield.