0% found this document useful (0 votes)
14K views

Generators in JavaScript

Generators in Javascpt

Uploaded by

Gagandeep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14K views

Generators in JavaScript

Generators in Javascpt

Uploaded by

Gagandeep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
Generators JS Introduction + JavaScript generators are special functions that can be paused and resumed during execution. * They use the yield keyword to produce values and control the flow of iteration. + Regular functions return only one, single value but Generators can return (“yield”) multiple values. + We'll discover what makes them unique, how they differ from regular functions, and why they are valuable additions to JavaScript. Example To create a generator, you need to first define a generator function with function* symbol. + When a it’s called, it doesn't immediately run its code. Instead, it returns a special object known as a "generator object.” Tas CuLe Curia Ome yield 1; et aya SPCC mh } Coa ecu gO console. log(gen.next()); // { value: 1, done: false } console. log(gen.next()); // { value: 2, done: false } console.log(gen.next()); // { value: 3, done: false } console.log(gen.next()); // { value: undefined, done: true } Passing Arguments Generators are not only great at producing values, but they're also excellent listeners! + They can receive values from the outside world and send them back too. function* twoWayGenerator() { Cea et Men eCPM ETM a (2 SECM CTR Tete CCT RTT SOT) mae } const gen = twoWayGenerator(); Pec eT TCIM Sst 0) // { value: "Please provide a value", done: false } console. log(gen.next(42)); // { value: "You provided: 42", done: false } ea ee (CaM t46 be // { value: undefined, done: true } iterating With Generators Generators have a hidden talent — they can create custom iterators! It's like giving them the power to control the flow of iteration. + Here's is the example how you can build custom iterators using generators. + We can loop over their values using for. .of . function* range(start, end, step) { peta emer hs e while (current <= end) { Ot a ta eee yi M CoS a eee UTC Ge for (const num of numbers) { console.log(num); // Output: 1, 3, 5, 7, 9 } Generator Delegation Generators have a talent for teamwork too! They can join forces and delegate tasks among themselves. - It's like assembling a dream team of generators. Sis Cea Cuchi ve met pet Bas Sot S UB } cis Ceaucie ya re ee yield 3; Set Ce ed i function* composedGenerator() { yield* generatorOne(); Bet Cane Ue hla Od const gen = composedGenerator(); console. log(gen.next()); console. log(gen.next()); console.log(gen.next()); i i aCe CLC ny eR CCeyar Leer eae { value: 3, done: false } { r u console. log(gen.next() console. log(gen.next() Se UCEae Cu reer value: undefined, done: true } Error Handling Error handling is essential in any application, and generators provide mechanisms to handle errors gracefully. * We'll learn how to handle errors within generator functions using try. . .catch blocks. function* errorGenerator() { try { SoCo APC K ead agar CR ar Turan ee yield 3; // This line will not be reached } catch (error) { yield “Error: ${error.message}”; } } Car Ceara tg 0 console. log(gen.next()); // { value: 1, done: false } console.log(gen.next()); // { value: 2, done: false } console. log(gen.next()); // { value: “Error: Oops, something went wrong!", done: false } console.log(gen.next()); // { value: undefined, done: true } Conclusion Generators are created by generator functions function* f(..) {..}. Inside generators (only) there exists a yield operator. The outer code and the generator may exchange results via next/yield calls. As always, | hope you enjoyed the post and learned something new. If you have any queries then let me know in the comment box.

You might also like