Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method for js to call generator

2025-03-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "what is the method of js calling generator". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Keep calling the next () method of the generator object, execute the generator code, and then return an object every time you encounter yieldx;, and then pause.

Var f = fib (5); f.next (); / {value: 0, done: false} f.next (); / / {value: 1, done: false} f.next (); / / {value: 1, done: false} f.next (); / / {value: 2, done: false} f.next (); / / {value: 3, done: false} f.next (); / / {value: undefined, done: true}

2. Iterate the generator object directly with for...of loop, and there is no need to judge done by yourself.

Function* fib (max) {var t, a = 0, b = 1, n = 0; while (n < max) {yield a; [a, b] = [b, a + b]; n + +;} return;} for (var x of fib (10)) {console.log (x) / / output 0, 1, 1, 2, 3,.} "what is the method of js calling generator" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report