25
loading...
This website collects cookies to deliver better user experience
yield
so whenever this keyword is found this means that a value is being generated by the generator function. function* basicGenerator() {
yield 1;
yield 2;
yield 3;
return 4;
}
// [object Generator]
function* basicGenerator() {
yield 1;
yield 2;
yield 3;
return 4;
}
let generator = basicGenerator();
let firstResult = generator.next();
console.log(firstResult);
// {value: 1, done: false}
basicGenerator()
function and it returned a generator object and we logged its output. The Generator object contains three main functions, a next()
to continue the execution and returns an object of value and done ( will discuss it in a moment ) and a throw()
that stops the generator's execution and throws an error and a return()
that finishes the execution the return a value.next()
function works, when we execute it the generator function will point to the next execution level or the next yield keyword and will return a value of the previous yield keyword. So in the above code the first next()
will return {value: 1, done: false}
and will point to the next yield that shall return 2 in the next execution.done
refer to? done will always be true until there are no more yields available for execution or the execution pointed to a return keyword, at that moment any next()
calls shall return an undefined value.function* basicGenerator() {
yield 1;
yield 2;
yield 3;
return 4;
}
let generator = basicGenerator();
let data = {};
while(!data.done) {
data = generator.next();
console.log(data.value);
}
// [1,2,3,4]
done : false
that indicates that there are no more executions available in our generator.for...of
could be used to iterate over the values of a generator like this:function* generateSequence() {
yield 1;
yield 2;
yield 3;
}
let generator = generateSequence();
for(let value of generator) {
console.log(value);
}
// 1 2 3
function* basicGenerator() {
let res = yield 1;
console.log(res); // Passing This
let res2 = yield 2;
console.log(res2); // Done Passing
yield 3;
}
const generator = basicGenerator();
generator.next();
generator.next("Passing This");
generator.next("Done Passing");
yield
keyword, we are also assigning a variable to its output, and when we call the generator next()
function we first pass no arguments ( the first next is by default will neglect any passed arguments ) and then we pass whatever we want, so the second next will have Passing This
passed to it and thus it will assign this value to the first variable in our execution which is res
and then we pass another argument and res2
shall receive the Done Passing
value.function* infiniteIdGenerator() {
let start = 0;
while (true) yield start++;
}
const generator = infiniteIdGenerator();
generator.next(); // 0
generator.next(); // 1
generator.next(); // 2
generator.next(); // 3
// ...Infinity
export function* throttle(func, time) {
let timerID = null;
function throttled() {
clearTimeout(timerID);
timerID = setTimeout(func.bind(window, arg), time);
}
while (true) throttled(yield);
}
const generator = throttle(() => console.log("some logic"), 300);
generator.next();
redux-saga
that allows it to create side-effects with generators.