In Web Development
Yield - Generator Functions (node.js) - read the full article about Node Js update, Web Development and from Talking about Computer Science on Qualified.One
Youtube Blogger
Do you know how to use generator functions in javascript? So this is the topic of this video We are going to see how to define a generator function using this syntax and what is its propose a function that can stop and return a value in the middle of its execution and in a next moment continuing the execution until finding another stopping point that will be this "yield" keyword and it will repeat until the function ends or find a return value or an exception is triggering. So lets experiment this functionality together. Here in the upper part of this code we have a generator function defined. Whats the difference between a generator function and a normal function? It is because it has this star at the side of the function keyword and inside its body, once its defined as a generator function, it can have the yield keyword, it could have more than one yield keyword, if you have more than one point of return. The idea is that the function will start to run and it will stop the running of the workflow of the function as soon as it finds a yield keyword, then the value at the right of the yield keyword will be returned for who has called the function. So here the function is just created and now with the next statement we take the next value its find, it means until the next yield keyword is found then it returns this value once its returning we can read the value and its blocked the function simply stop to execute. Then in the next time we call the next again it will run the line below and it will keep running until it finds one the yield keyword, it finds that, then the value could be returned and its again stop it and then if we call the next value but it do not find the yield keyword but finds the end of the function, what happens? It will ends in this case the undefined value will be returned, because when the function ends without the return keyword it returns undefined. It its the same this function the way it is its the same as do that the same as do that, okay? So having an undefined return or no return is the same. So in this example here it will be returned undefined because it will end the execution, otherwise if you want to return a different value we just can return here something different like a zero, okay ? Lets try that together. Lets execute this function that will be created with this constructor, this seed, like zero, so we are going to execute that and okay, so here first execution its start with zero execute until arrives in the yield keyword and return the on value that that has entered zero. Then we call the next again and what happens? Index was incremented by one its twwo less than two and then stop in the next yield and return the one there and then we call again the next value what happened it was increased by 2 2 but when arrives here in the yield 2 is not smaller than 2 then it jumps to out and it was not returning the number two, it was returned then undefined , because theres no return statement. If we want to return the index value anyway we could for instance just return here the index variable and now instead of undefined. Lets see what we are going to receive we clear that, we execute that again, and you see now it returns two, why? Because it really has incremented by one after the one so it reads the value two it jumps the while statement because now it does not match the condition but it really was two and then return the latest value of the variable and the function ends and the generator is over, okay? So this was one overview about how to use the generator functions. Remember of these key aspects: the star at the side of the function name; the yield keyword; the next method; and the value property. With these concepts you are able to create your own generated functions. You can download this source code we have demonstrated by the link available below in the video description. Thank you for watching!!!
Talking about Computer Science: Yield - Generator Functions (node.js) - Web Development