Why this generator function in JavaScript is stopping before any yield expression?












2















Code:



function* logGenerator() {
console.log(0);
console.log(1, yield);
console.log(2, yield);
console.log(3, yield);
}

var gen = logGenerator();
gen.next('alpha');
console.log('mark');
gen.next('beta');


Output:



0
mark
1 beta


but, why not:



0
1 alpha
mark
2 beta


In the line: gen.next('alpha');
the code only logs '0' and stops but does not reach the first yield expression, while the actual should have been '0n1 alpha'.



And 'alpha' is never logged.










share|improve this question

























  • thanx for help @Lloyd Nicholson

    – PranshuKhandal
    Jan 18 at 19:26
















2















Code:



function* logGenerator() {
console.log(0);
console.log(1, yield);
console.log(2, yield);
console.log(3, yield);
}

var gen = logGenerator();
gen.next('alpha');
console.log('mark');
gen.next('beta');


Output:



0
mark
1 beta


but, why not:



0
1 alpha
mark
2 beta


In the line: gen.next('alpha');
the code only logs '0' and stops but does not reach the first yield expression, while the actual should have been '0n1 alpha'.



And 'alpha' is never logged.










share|improve this question

























  • thanx for help @Lloyd Nicholson

    – PranshuKhandal
    Jan 18 at 19:26














2












2








2


1






Code:



function* logGenerator() {
console.log(0);
console.log(1, yield);
console.log(2, yield);
console.log(3, yield);
}

var gen = logGenerator();
gen.next('alpha');
console.log('mark');
gen.next('beta');


Output:



0
mark
1 beta


but, why not:



0
1 alpha
mark
2 beta


In the line: gen.next('alpha');
the code only logs '0' and stops but does not reach the first yield expression, while the actual should have been '0n1 alpha'.



And 'alpha' is never logged.










share|improve this question
















Code:



function* logGenerator() {
console.log(0);
console.log(1, yield);
console.log(2, yield);
console.log(3, yield);
}

var gen = logGenerator();
gen.next('alpha');
console.log('mark');
gen.next('beta');


Output:



0
mark
1 beta


but, why not:



0
1 alpha
mark
2 beta


In the line: gen.next('alpha');
the code only logs '0' and stops but does not reach the first yield expression, while the actual should have been '0n1 alpha'.



And 'alpha' is never logged.







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 0:38









Lloyd Nicholson

296111




296111










asked Jan 1 at 19:06









PranshuKhandalPranshuKhandal

430115




430115













  • thanx for help @Lloyd Nicholson

    – PranshuKhandal
    Jan 18 at 19:26



















  • thanx for help @Lloyd Nicholson

    – PranshuKhandal
    Jan 18 at 19:26

















thanx for help @Lloyd Nicholson

– PranshuKhandal
Jan 18 at 19:26





thanx for help @Lloyd Nicholson

– PranshuKhandal
Jan 18 at 19:26












1 Answer
1






active

oldest

votes


















4














You're understanding of where the generator stops is not correct. To be fair, it's one of the most confusing aspects of generators. A generator stops immediately when it hits yield even if that's in the middle of an expression.



So in your example:



console.log(0);        // logs 0
console.log(1, yield); // evaluates the parameters to console.log, sees yield and stops before running console.log


You never make it to the console.log() because it hits yield while evaluating the parameters. It will continue and finish the log on the next next(). This is why generators are often "primed" with an initial next() call if they are going to depend on values passed in. The first call of next() can't pass a value.



Here's a simpler example showing the issue:






let test = "start"

function* logGenerator() {
console.log("genertator starting")
test = yield
}

var gen = logGenerator();
// call initial next. `val1` is lost to priming the generator
gen.next('val1');

// the generator stops before the assignment to test
// test is still "start"
console.log(test)

// only on the second call can you get a value to test
gen.next('val2');
console.log(test)








share|improve this answer


























  • thanks:-) sorry for so much late response! i'm new to stackoverflow

    – PranshuKhandal
    Jan 18 at 19:23






  • 1





    thanks for the help

    – PranshuKhandal
    Jan 18 at 19:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998180%2fwhy-this-generator-function-in-javascript-is-stopping-before-any-yield-expressio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














You're understanding of where the generator stops is not correct. To be fair, it's one of the most confusing aspects of generators. A generator stops immediately when it hits yield even if that's in the middle of an expression.



So in your example:



console.log(0);        // logs 0
console.log(1, yield); // evaluates the parameters to console.log, sees yield and stops before running console.log


You never make it to the console.log() because it hits yield while evaluating the parameters. It will continue and finish the log on the next next(). This is why generators are often "primed" with an initial next() call if they are going to depend on values passed in. The first call of next() can't pass a value.



Here's a simpler example showing the issue:






let test = "start"

function* logGenerator() {
console.log("genertator starting")
test = yield
}

var gen = logGenerator();
// call initial next. `val1` is lost to priming the generator
gen.next('val1');

// the generator stops before the assignment to test
// test is still "start"
console.log(test)

// only on the second call can you get a value to test
gen.next('val2');
console.log(test)








share|improve this answer


























  • thanks:-) sorry for so much late response! i'm new to stackoverflow

    – PranshuKhandal
    Jan 18 at 19:23






  • 1





    thanks for the help

    – PranshuKhandal
    Jan 18 at 19:30
















4














You're understanding of where the generator stops is not correct. To be fair, it's one of the most confusing aspects of generators. A generator stops immediately when it hits yield even if that's in the middle of an expression.



So in your example:



console.log(0);        // logs 0
console.log(1, yield); // evaluates the parameters to console.log, sees yield and stops before running console.log


You never make it to the console.log() because it hits yield while evaluating the parameters. It will continue and finish the log on the next next(). This is why generators are often "primed" with an initial next() call if they are going to depend on values passed in. The first call of next() can't pass a value.



Here's a simpler example showing the issue:






let test = "start"

function* logGenerator() {
console.log("genertator starting")
test = yield
}

var gen = logGenerator();
// call initial next. `val1` is lost to priming the generator
gen.next('val1');

// the generator stops before the assignment to test
// test is still "start"
console.log(test)

// only on the second call can you get a value to test
gen.next('val2');
console.log(test)








share|improve this answer


























  • thanks:-) sorry for so much late response! i'm new to stackoverflow

    – PranshuKhandal
    Jan 18 at 19:23






  • 1





    thanks for the help

    – PranshuKhandal
    Jan 18 at 19:30














4












4








4







You're understanding of where the generator stops is not correct. To be fair, it's one of the most confusing aspects of generators. A generator stops immediately when it hits yield even if that's in the middle of an expression.



So in your example:



console.log(0);        // logs 0
console.log(1, yield); // evaluates the parameters to console.log, sees yield and stops before running console.log


You never make it to the console.log() because it hits yield while evaluating the parameters. It will continue and finish the log on the next next(). This is why generators are often "primed" with an initial next() call if they are going to depend on values passed in. The first call of next() can't pass a value.



Here's a simpler example showing the issue:






let test = "start"

function* logGenerator() {
console.log("genertator starting")
test = yield
}

var gen = logGenerator();
// call initial next. `val1` is lost to priming the generator
gen.next('val1');

// the generator stops before the assignment to test
// test is still "start"
console.log(test)

// only on the second call can you get a value to test
gen.next('val2');
console.log(test)








share|improve this answer















You're understanding of where the generator stops is not correct. To be fair, it's one of the most confusing aspects of generators. A generator stops immediately when it hits yield even if that's in the middle of an expression.



So in your example:



console.log(0);        // logs 0
console.log(1, yield); // evaluates the parameters to console.log, sees yield and stops before running console.log


You never make it to the console.log() because it hits yield while evaluating the parameters. It will continue and finish the log on the next next(). This is why generators are often "primed" with an initial next() call if they are going to depend on values passed in. The first call of next() can't pass a value.



Here's a simpler example showing the issue:






let test = "start"

function* logGenerator() {
console.log("genertator starting")
test = yield
}

var gen = logGenerator();
// call initial next. `val1` is lost to priming the generator
gen.next('val1');

// the generator stops before the assignment to test
// test is still "start"
console.log(test)

// only on the second call can you get a value to test
gen.next('val2');
console.log(test)








let test = "start"

function* logGenerator() {
console.log("genertator starting")
test = yield
}

var gen = logGenerator();
// call initial next. `val1` is lost to priming the generator
gen.next('val1');

// the generator stops before the assignment to test
// test is still "start"
console.log(test)

// only on the second call can you get a value to test
gen.next('val2');
console.log(test)





let test = "start"

function* logGenerator() {
console.log("genertator starting")
test = yield
}

var gen = logGenerator();
// call initial next. `val1` is lost to priming the generator
gen.next('val1');

// the generator stops before the assignment to test
// test is still "start"
console.log(test)

// only on the second call can you get a value to test
gen.next('val2');
console.log(test)






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 19:25

























answered Jan 1 at 19:13









Mark MeyerMark Meyer

39.1k33162




39.1k33162













  • thanks:-) sorry for so much late response! i'm new to stackoverflow

    – PranshuKhandal
    Jan 18 at 19:23






  • 1





    thanks for the help

    – PranshuKhandal
    Jan 18 at 19:30



















  • thanks:-) sorry for so much late response! i'm new to stackoverflow

    – PranshuKhandal
    Jan 18 at 19:23






  • 1





    thanks for the help

    – PranshuKhandal
    Jan 18 at 19:30

















thanks:-) sorry for so much late response! i'm new to stackoverflow

– PranshuKhandal
Jan 18 at 19:23





thanks:-) sorry for so much late response! i'm new to stackoverflow

– PranshuKhandal
Jan 18 at 19:23




1




1





thanks for the help

– PranshuKhandal
Jan 18 at 19:30





thanks for the help

– PranshuKhandal
Jan 18 at 19:30




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998180%2fwhy-this-generator-function-in-javascript-is-stopping-before-any-yield-expressio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory