The letter S in a light blue, stylized speech bubble followed by SpeakBits
SpeakBitsThe letter S in a light blue, stylized speech bubble followed by SpeakBits
Trending
Top
New
Controversial
Search
Groups

Enjoying SpeakBits?

Support the development of it by donating to Patreon or Ko-Fi.
About
Rules
Terms
Privacy
EULA
Cookies
Blog
Have feedback? We'd love to hear it!

JavaScript Generators Explained, But On A Senior-Level

reactsquad.io
submitted
9 mos ago
bythrowschentoprogramming

Summary

The short answer to the question "What is a generator?" is:Generators are pull streams in JavaScript. You will see code examples for pull streams soon when you're going to see generator code.

Promises in JavaScript exhibit eager evaluation for several reasons. Eager means data is evaluated immediately, regardless of whether the result is needed in that moment. LazyLazy means only evaluated when the value is needed (not before)

The processData function never runs and you never see "Processing 5" This shows that the expression only evaluates what is needed to get the result.

Function* numberGenerator() yields values based on the input it receives. If a value is not found, the generator is considered to be done and marks it as such. If the generator fails to yield a value, it is marked as "unusable"

Sagas are a prime example of handling asynchronous I/O operations. But you're going to learn how to use sagas in a future article.

The position of the main user in the test is unknown in the sortedUsers array. This allows the test to dynamically assign each user a unique role from a predefined list.

 web site website internet site site comic book book jacket dust cover dust jacket dust wrapper cash machine cash dispenser automated teller machine automatic teller machine automated teller automatic teller ATM-0
14

4 Comments

2
joseph
9 mos ago
I'll be honest: I always struggle to find a use for generators
2
justadev
9 mos ago
Streaming items of an unknown quantity. If you don't know when the user will stop searching for the type of time and you don't know how many you have, this is a perfect use case.
2
joseph
9 mos ago
I think I need to rewrite some of my functions then
2
justadev
9 mos ago
Not everything needs to be this way, sometimes good old fashioned loops are good enough