accessing yield inside arrow function [closed]

2019-09-22 03:03发布

问题:

since it it not possible to create a arrow generator function, yield is never used in the context of a arrow function.

var arrowGenerator = *() => { };

then you should be able to and use yield in the context of the generator function. just like this

function* generator() {
    funcWithCallback((value) => {
       yield value;
    });
}

but in babel it uses yield in context of the arrow function and not the genreators.

i want to do this so you don't need to return a callback function with the value, just to yield it.

function* gen() {
    yield function (callback) {
        funcWithCallback(callback);
    } 
}

回答1:

The yield and yield* keywords can only be used directly inside a generator function. Your code fragment is conceptually flawed in a similar manner to:

function f1() {
  if(someCondition) {
    f2((value) => {
       else {
         // do something
       }
    });
  }
}

or, to this:

function f1() {
  f2((value) => {
    return someValue; // while this is legal, it doesn't cause f1 to return
  });

  codeAfterReturn();
}

Obviously, these 2 examples don't "work", and so does your code fragment.