1

I came across this code while learning about yield and wondering what yield would do as a argument to a function . Looks like a glorified return in this place

export function * throttle(func, time) {
 let timerID = null;
 function throttled(arg) {
 clearTimeout(timerID);
 timerID = setTimeout(func.bind(window, arg), time); // what does this do????
 }
 while(true) throttled(yield);
}
export class GeneratorThrottle {
 constuctor() {};
 start = () => {
 thr = throttle(console.log, 3000);
 thr.next('');
 };
 toString = () => {
 console.log(throttle);
 console.log('start =', this.start);
 };
};
asked Sep 3, 2021 at 14:41
1

1 Answer 1

1

With next method, You can pass data as argument into generator function.

function* logGenerator() {
 console.log(0);
 console.log(1, yield);
 console.log(2, yield);
 console.log(3, yield);
}
var gen = logGenerator();
// the first call of next executes from the start of the function
// until the first yield statement
gen.next(); // 0
gen.next('pretzel'); // 1 pretzel
gen.next('california'); // 2 california
gen.next('mayonnaise'); // 3 mayonnaise

And func.bind(window, arg) are just fancy way to call it with argument,

where bind method return a function... And you can access this as window in that function...

Example:

function func(args) {
 console.log(this, args)
}
func.bind("this", "args")();

answered Sep 3, 2021 at 15:18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.