Тёмный
No video :(

Generators in JavaScript 

Fun Fun Function
Подписаться 263 тыс.
Просмотров 49 тыс.
50% 1

🔗 Last weeks episode about Iterators in JavaScript
• Iterators in JavaScrip...
💖 Tiptapp - work with React Native in the heart of Stockholm
tiptapp.com/fff
🔗 Fun Fun Forum topic for the video
www.funfunforu...
🔗 Quokka, the funky inline evaluation tool used in the video
quokka.funfunfu...
🔗 Support the show by becoming a Patreon
• Patreon + Fun Fun Foru...
🔗Code from the episode
github.com/mpj...
🔗 mpj on Twitter
/ mpjme
🔗 Help translate the show to your language
www.youtube.com...
In last weeks video, we looked at Iterators in JavaScript. In this video,
we're going to look at Generators in JavaScript, which is a super weird
concept until you realise that Generators are just thin,
syntactic sugar to create JavaScript Iterators.

Опубликовано:

 

26 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 132   
@Dalendrion
@Dalendrion 6 лет назад
Wait. You wrote `return {value: "hardy the dog", done: true}` Is that correct? It works when you call the iterator manually, but how about a for loop? I would expect the following code to print "hardy the dog". But it doesn't! ``` function printlnFunction(element, str) { element.textContent += str + " "; } const println = printlnFunction.bind(undefined, document.getElementById("someDragons")); const iterable = { [Symbol.iterator]: () => { let iterations = -1; return { next: () => { iterations++; switch (iterations) { case 0: return {value: "fluffykins some dragon", done: false}; case 1: return {value: "mark the fine dragon", done: false}; case 2: return {value: "hardy the dog", done: true}; } } }; } }; for (const i of iterable) { println("- " + i); } ```
@funfunfunction
@funfunfunction 6 лет назад
Fantastic catch! I'll try set things straight on how exactly one is supposed to set done in upcoming videos.
@harshshah85
@harshshah85 6 лет назад
This is because before it gets the 'value', it check of 'done'. And if its 'done' then the 'value' is NOT read and it just returns to the main function
@MrMate1212
@MrMate1212 Год назад
Over 4 years ago and this video is still one of the bests about generators :)
@domski196
@domski196 6 лет назад
I liked that hardy the dog could participate because JS is weakly typed haha
@erikgurney306
@erikgurney306 6 лет назад
I’m not a newb, but no other explanation of generators made me understand and want to look into them more. Good stuff!
@andrewkiminhwan
@andrewkiminhwan 6 лет назад
you are certainly the most eccentric coding youtuber i know of! It took me a while to appreciate how much I learn from your style of teaching, thank you for being you!
@Ben-rh7mf
@Ben-rh7mf 6 лет назад
Great video. I’m looking forward to the async generators and iterators videos. Hopefully those are .next()
@prox2103
@prox2103 6 лет назад
I second this
@IAMZERG
@IAMZERG 6 лет назад
I see what you did there. Well .done
@chrsbll
@chrsbll 6 лет назад
Occasionally your videos go just a little over my head - however this is by far the best explanation of generators I've seen.
@CoryTheSimmons
@CoryTheSimmons 6 лет назад
Definitely interested in learning more about how generators can be used in real-world async situations (to await certain content and populate the DOM accordingly). I've heard generators are more powerful than async/await (i.e. "redux-saga is better than redux-thunk + async/await") but have no idea why and
@papawattu
@papawattu 6 лет назад
+1
@RestfulCoder
@RestfulCoder 4 года назад
I stumbled into generators when using React sagas. Thanks for this detailed explanation of generators, and how they relate to iterators. Great work!
@oscarcrespo3313
@oscarcrespo3313 6 лет назад
This video is awesome! I've been trying to understand Redux-Sagas for a while, it all makes sense now!
@Cobesz
@Cobesz 6 лет назад
Please, continue! This is amazing stuff :D
@asimsinthehouse
@asimsinthehouse 6 лет назад
Thanks linking me to this mpj! Really cleared up generators!
@Ratstail91
@Ratstail91 5 лет назад
Thanks! I didn't really know what generators were all about until now.
@BLACKNIGHTCODING
@BLACKNIGHTCODING 6 лет назад
Yep, keep going. Let's see how deep the rabbit hole goes!!
@Finicky9
@Finicky9 6 лет назад
Gooooood monday morning!
@luluozer
@luluozer 6 лет назад
You can copy a line by just doing copy with the cursor on that line. So if you want to duplicate a line you can easily command c command v it and it’ll be doubled. Means you don’t need to select the each time. Will save you a few seconds 😃
@funfunfunction
@funfunfunction 6 лет назад
Thanks! You're right, but I intentionally stay away from hotkeys etc. in the videos as much as I can. Any time I use a tool etc in the videos I need to also spend time explaining it, and even when I do that, it distracts from the subject at hand.
@kmylodarkstar2253
@kmylodarkstar2253 4 года назад
Thanks for all content, hope you have time for breath your mind.
@Alessandro-nq3tm
@Alessandro-nq3tm 6 лет назад
We want asynchronous generators !!! When we want them ? Nooww :)
@BowlineDandy
@BowlineDandy 6 лет назад
... now!!
@sr_aman
@sr_aman 6 лет назад
Yesss!
@nathancornwell1455
@nathancornwell1455 4 года назад
You totally missed any opportunity to say "We want them .next( ) ! "
@joemiller1057
@joemiller1057 6 лет назад
super cool! love learning new JS stuff please continue w/ iterators && generators :)
@kondovwrites
@kondovwrites 6 лет назад
I've been using generators lately in the context of redux-saga, quite useful. Looking forward to more content on similar topics!
@jedrzejsadowski6810
@jedrzejsadowski6810 6 лет назад
Hi. Would be great to hear about yield* in the next video. It was really mind-blowing for me when I learnt about it :D
@prox2103
@prox2103 6 лет назад
Amazing job MPJ!!! It finally makes sense :)
@rikilamadrid
@rikilamadrid 6 лет назад
after I saw the intro i was almost expecting you to take a sip out of the mug as a beginning of the lesson...
@martinalcala4823
@martinalcala4823 4 года назад
Implementation of python's range in JavaScript! const range = function*(start = 0, end, step = 1) { if (!end) { end = start; start = 0; } while(true) { const value = start; start += step; if (value >= end) return; yield value; } }; // 0, 1, 2, 3, 4 for (const x of range(5)) { console.log(x); }
@trevorasargent
@trevorasargent 6 лет назад
this is the only explanation of iterators (in any language) that has made and piece of g-- d--- sense to me. you ruuuuule. keep it coming I'm here for asynccccc
@Kowbinho
@Kowbinho 6 лет назад
I merely heard about generators but now that I see it moving I begin to see how it could be powerful and at the same time really not rocket science. Thanks for the show !
@ggcadc
@ggcadc 6 лет назад
I presented about generators to my local Javascript meetup and couldn't find any real solid use cases for them. I mean, they are useful in some cases but there seem to be better solutions in most of those cases. Id love to see something where the generator is the BEST solution to the problem.
@funfunfunction
@funfunfunction 6 лет назад
I did not see one either until I saw async iterators being used to stream process data in ObservableHQ (beta.observablehq.com/@mbostock/introduction-to-asynchronous-iteration), then they arge bloody magnificent. We're going to be looking at that coming up, but we need to work ourselves up to that point first.
@madflanderz
@madflanderz 6 лет назад
For me the best part of using generators (like redux-saga) is, that it makes it super easy to test different parts of your generator logic without the pain of mocking functions etc. Before redux-saga i used redux-thunk and to test this you need a lot of dependency injection. With redux-saga it feels much easier and straight forward.
@iorbit
@iorbit 6 лет назад
"But Hardy the dog is not a dragon!? We don't mind. This is a weakly typed language!" ^ why I love this show
@coconutbunch
@coconutbunch 6 лет назад
Good Tuesday Morning, I have been following your tutorials or rather to say fun learning sessions for a while now. There is always a new feature or shortcut or how-to-dos, i learn, eg: quokka here(I never even had an idea of its existence). Teaching is art and you have nailed it. Generators is one of the best addition to ES6. And I would excited to learn how can they work async. Also I would appreciate if you can share on design aspects/Best use cases with Node.js, angular.js,react.js ... other popular frameworks. Thank you very much. :)
@gayusjuliuscaesarsalad
@gayusjuliuscaesarsalad 6 лет назад
Monday, home from school -> first watch some FUNFUNFUNCTION!!! :D 👍
@lutaseb
@lutaseb 6 лет назад
When i used iterators with java, usually you had a hasNext() method returning if the iterator was done or not, my question then is, it looks like it s good to return your last iterator value with the flag done set to true (and undefined furthr on), am i right? I mean, what's the usual way of using an iterator then ? btw, those 2 last viddeos are the best you ve ever done to me! Awesome!
@funfunfunction
@funfunfunction 6 лет назад
GREAT question. Looking at the Mozilla developer documentation on the topic (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol), it seems like one is actually not allowed to have the value property set to a non-object: "The next method always has to return an object with appropriate properties including done and value. If a non-object value gets returned (such as false or undefined), a TypeError (“iterator.next() returned a non-object value”) will be thrown."
@osakaandrew
@osakaandrew 6 лет назад
Isn't it interesting that it also mirrors the behaviour of for loops. If you run a plain for loop with a condition like `i < 100`, then even though inside the loop the value of i never is more than 99, its value after the loop has completed is 100, not 99 (and this is what the spec says should happen). I never found that behaviour normal, but now I see that it is at least consistent.
@lutaseb
@lutaseb 6 лет назад
hummm i just went to look at the doc u mentionned, it just says "The next method always has to return an object with appropriate properties including done and value", which just says you have to return {done, value} objects, but id does not specify if the last values sent back could be {value: "last", done: true} or {value:"last", done: false}, followed by {value: undefined, done: true}, since we don't have the hasNext() methods in java that allows you to know in advance if the iterator is over, i m still wondering. Personnaly in this case i would go for the last value as {value:"last, done: true}
@matthiasmuller5211
@matthiasmuller5211 6 лет назад
I think the question was if it's possible to move the logic, whether another dragon shall be created, into the iterator call BEFORE the one where it's actually created and return done: false only if there will be another dragon. This way the consumer of the iterator knows whether he will get a dragon before he actually calls it (that's basically the information, that the java hasNext() method contains).
@yasinyaqoobi
@yasinyaqoobi 6 лет назад
That was actually my first question too when I came across generators. If u look into stackoverflow you will find how we added a hasnext method. I think this should have been added in the core.
@krypto7264
@krypto7264 6 лет назад
There you go 4:41, a drawback of Quokka
@VladimirBrasil
@VladimirBrasil 6 лет назад
Beautiful. The "build your own iterator through a factory using the reserved key [Symbol.iterator]" is a very good example of a way to maintain the language flexible enough so everyone can add its own iterator, without changing every part of the language that simply use any of these iterators. I am not sure if this would be called an interface by the old OOP guys. But I am almost sure that this respects a beautiful easy-maintenance principle, the "open-closed principle". Open for adding new capabilities, closed to change current code. Couldn't we use the same technique that the "iterator" technique uses to architect our programs from the beginning? Probably there already is an episode on that, I am sorry for not knowing it yet. Congrats on a very-well-perfect show.
@smackjaxcodes4791
@smackjaxcodes4791 5 лет назад
"And then we yield...Mark..." Awesome :)
@forgettd
@forgettd 6 лет назад
This kind of stuff is what I have subscribed for. Even if I do already know about the stuff, you're talking I won't skip the video. Thank you, mpj, for creating such interesting project. And for being hella attractive too :)
@yantakushevich1014
@yantakushevich1014 6 лет назад
Great series. Give us more!
@vandhuymartins9481
@vandhuymartins9481 6 лет назад
Great video as always :) Thank you MPJ!
@ShingoSAP
@ShingoSAP 6 лет назад
Really cool man! Very interesting on async Iterators and Generators!
@MJ-ke1xe
@MJ-ke1xe 6 лет назад
Im just here for all the different types of glasses
@brucedonovan
@brucedonovan 6 лет назад
Hi! Thanks for yet another great vid! Quick question: Can you 'yield' anything (for example a function or another generator)?
@funfunfunction
@funfunfunction 6 лет назад
Yes. You can yield anything. This allows us to do really, really funky things with generators. When Generators arrived on the scene, async / await had not made it into the language, so the main big thing that the community used generators for was to implement async / await.... That said... it's a reeaaaaallly slippery slope though and often results in code that has so many layers of indirection that it's a lot harder to reason about than it would have been without iterators or generators, defeating the purpose of them. Generators are one of those things that are insanely cool and makes you feel incredibly clever, which is a good sign that one should look really critically at the code one produces and ask "is this really clearer?"
@brucedonovan
@brucedonovan 6 лет назад
Thanks MPJ!
@hpgildwel
@hpgildwel 3 года назад
I know this is probably a style thing, but why not use a switch statement in the rewrite case? Its cleaner.
@michal3833
@michal3833 2 года назад
thank you your videos are so well explained and funny!
@naseer142
@naseer142 4 года назад
why can't we use for-in loop for the dragonArmy object ?
@codingpoet-ksp9468
@codingpoet-ksp9468 6 лет назад
A complement to the previous iterators episode and this episode with async iterators: gist.github.com/p10ns11y/d341ad364bd0d6e89df91342e5fc7aa5
@MissECylon
@MissECylon 4 года назад
Why do we need the Math.random() function? And that has to be set as bigger than any specific number? I don't understand that conditional line. :/
@int21
@int21 6 лет назад
This what I call quality content 👏 question what will be a real life applications for generators + yield?
@Joroze95
@Joroze95 6 лет назад
Please teach about RXJS and Observables!
@LiorCHAMLA
@LiorCHAMLA 6 лет назад
Gooood monday morning !
@MitraNami
@MitraNami 3 года назад
shouldn't we write 'Math.random()
@skyecairo6997
@skyecairo6997 6 лет назад
I like this hair style. It's 90's David Bowie cool.
@grumpycoder4592
@grumpycoder4592 4 года назад
They should be called "Iterator Generator Functions"
@youngsuit
@youngsuit 5 лет назад
fun fun function headspace the crocodile hunter 30% good content, 70% sexy accent
@solmazk7418
@solmazk7418 6 лет назад
Please make a video about how view engines work.Specially with nodejs
@funfunfunction
@funfunfunction 6 лет назад
Not sure what you mean when you say view engine, sorry.
@rinatvaliullov3247
@rinatvaliullov3247 6 лет назад
Cool, thanks, mpj! Can you record video about regural expressions in JS?
@cagmz
@cagmz 6 лет назад
11:19 very subtle, mister.
@ANANANTN
@ANANANTN 6 лет назад
👍👍 async iteration & generators
@TarrenHassman
@TarrenHassman 6 лет назад
Stolkholm is a perfect place for anyone using react.js, because the only reason they might be using it is if they have stolkholm syndrome.
@jihadkhorfan
@jihadkhorfan 6 лет назад
async generators with some API calls plz
@yurioliveira6603
@yurioliveira6603 6 лет назад
Good pace, nice code!
@max8hine
@max8hine 6 лет назад
Generators and Promise / Async Function in Async action?
@cannabanana5929
@cannabanana5929 6 лет назад
What is the extensionn being used to show the results/value of func or variable?
@funfunfunction
@funfunfunction 6 лет назад
Its mentioned in the epsiode right after I show it for the first time :) also linked in the epsiode desc.
@GeorgeMadiarov
@GeorgeMadiarov 6 лет назад
I'm a bit too dumb and still dont get it, can you make a video with real-life example with fetch/axios or other case scenarios?
@funfunfunction
@funfunfunction 6 лет назад
We haven’t started talking about async scenarios yet, so you couldn’t possibly get that scenario, don’t worry. We’ll get to it.
@livethetube
@livethetube 6 лет назад
Where would you use it?
@funfunfunction
@funfunfunction 6 лет назад
When you want to make things iterable.
@jakubrpawlowski
@jakubrpawlowski 6 лет назад
Add in description an affiliate link to buy that dotted notebook please.
@funfunfunction
@funfunfunction 6 лет назад
Oh thats an idea. Its a lechturm 1917
@venil82
@venil82 5 лет назад
Not covered, yield can return value passed from outside world
@RutgerWillems
@RutgerWillems 6 лет назад
Link in description links to this video, not the Iterator one.
@funfunfunction
@funfunfunction 6 лет назад
thanks!
@a.rnaseef6489
@a.rnaseef6489 6 лет назад
How this is different from async/await...
@funfunfunction
@funfunfunction 6 лет назад
Generators is what is used under the hood to make async/await. Async/await is a very specific implementation of generator - before async / await existed in the language many used generators to implement it. But Generators are more general - async/await is limited to returning one single result, a generator can keep yielding, so it can be used to implement streams.
@a.rnaseef6489
@a.rnaseef6489 6 лет назад
cool, thanks mate. :)
@spyrospapaioannou5724
@spyrospapaioannou5724 6 лет назад
Thanks for the video, you inspired me to make some snippets and learn the iterators-generators better. Great Work! My observation is that the function "someDragons" makes your case when you use ".next" to consume the values. If I am not mistaken, It would not work when you try to consume the returned value by a "for of" loop, as you showed at the start of the video. A generator function meets both requirements, by having the [Symbol.iterator] property. I think it would be useful to add to the iterator object the following: [Symbol.iterator]: function() { return this; } codesandbox.io/s/jzj0y8pwo9
@r-i-ch
@r-i-ch 6 лет назад
Want to learn ALL OF THE THINGS!
@fort1Z
@fort1Z 6 лет назад
Do you use generators in production?
@funfunfunction
@funfunfunction 6 лет назад
Generators has been around for several years now and can be safely used in production, and is by many people.
@SolidousMdz
@SolidousMdz 6 лет назад
+Fun Fun Function aaaaaaannd... thank you.
@beauremus
@beauremus 6 лет назад
Async all the things!
@ABHISHEK0058
@ABHISHEK0058 6 лет назад
Awesome man
@Blast-Forward
@Blast-Forward 5 лет назад
5:06 20:40
@irockalonso
@irockalonso 6 лет назад
what is this text editor ?
@funfunfunction
@funfunfunction 6 лет назад
Its vs code
@irockalonso
@irockalonso 6 лет назад
I think the video is excellent !. it's cool to wake up every morning with more of this... thank you very much!
@loki6841
@loki6841 6 лет назад
and he is using quokka plugin
@PSTRNC
@PSTRNC 6 лет назад
Your JUST is so funny i YAST cant stop smirking
@piotr-ciazynski
@piotr-ciazynski 6 лет назад
This video is much much better and clearer than your previous video about generators, here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ategZqxHkz4.html I think the video which you made two years ago is more about how async/await work under the hood using generators, not about generators itself, right? But this year is much better, you clearly explained what iterators are, and then you explained what generators are, without bothering about promises. Thanks!
@funfunfunction
@funfunfunction 6 лет назад
Yep. Two years of learning both for myself and the community. When it arrived on the scene people basically used them to implement async/await.
@MrPlaiedes
@MrPlaiedes 6 лет назад
I dig this episode a lot but while(true) feels wrong, even if just for explaining iterators..
@funfunfunction
@funfunfunction 6 лет назад
It's not just for explaining them - it's really how you do it. It's feels a bit weird, yes, but in a world that is pausable it makes a lot of sense.
@ZekeLnez
@ZekeLnez 6 лет назад
Didn't you make a video before talking about generators being kind of pointless? Or was that someone else?
@ZekeLnez
@ZekeLnez 6 лет назад
Found it. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ategZqxHkz4.html
@funfunfunction
@funfunfunction 6 лет назад
Yeah, I made a previous video on generators where I was not very excited about them. I was mostly because at that point, all the community did was to implement a poor mans async / await, and all the tutorials was about that, and I find the value of async / await over promises, even when implemented natively, to be pretty limited. However, lately, I've been using observablehq.com a lot, and when you use async iterables when processing large amounts of data it becomes insanely nice.
@ZekeLnez
@ZekeLnez 6 лет назад
Fun Fun Function fair enough. That makes sense. And thanks for the reply! As a side note, I've been a developer for about 15 years and I also came from a background of C# and Java. But I've always kind of hated JavaScript and other loosely typed scripting languages (though I've worked almost exclusively with it for three years now due to requirements of my job). But after listening to your musing on your opinion of JavaScript having come from a background of C#... I'm wondering if my dislike is coming from the stereotype of JavaScript not being a "real" programming language. As I dive deeper into it at work and grow even more comfortable with it. I completely agree with you. I feel far more productive with it. At the end of the day I consistently have something to show for it whereas with C# I could go a few days and only have laid the groundwork for a future awesome feature... thanks again for sharing your thoughts and wisdom. I watch every week.
@marijanikolic4546
@marijanikolic4546 6 лет назад
That sexy voice is still there I hear... :D Will there be more videos about functional programming? Or maaaaybe something about ReasonML? :) Thanks for keeping learning and confusion fun :)
@alvechy
@alvechy 6 лет назад
10:03 :D you're the best
@ulissemini5492
@ulissemini5492 5 лет назад
who else saw "dragon army" and thought of enders game?
@goodev
@goodev 6 лет назад
You are awesome
@jonathanfoster746
@jonathanfoster746 6 лет назад
Hurt your hand?
@funfunfunction
@funfunfunction 6 лет назад
yeah, have no idea how
@GalaxiaDeFavio
@GalaxiaDeFavio 6 лет назад
Hey
@ognjetina
@ognjetina 6 лет назад
what the hell bite you on your hand...
@ognjetina
@ognjetina 6 лет назад
actually I don't wanna know...
@funfunfunction
@funfunfunction 6 лет назад
I have no idea, to be honest :)
@mrgerbeck
@mrgerbeck 6 лет назад
I prefer a more functional style. Simply define a function returning a function that returns your items
@funfunfunction
@funfunfunction 6 лет назад
That would not be an accurate translation to functional style. The functional variant of a generator would be a stream, i.e. a function where you pass in a callback function that gets called for every item in the stream. I have an episode on streams here (ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-UD2dZw9iHCc.html) You might ask - why not just return the items - i.e. process all the items at once? There might be many reasons, but the most common one where we use a stream or generator is if we can't process everything at once in memory, for example, if we're processing a 400GB dataset.
@mrgerbeck
@mrgerbeck 6 лет назад
I mean something like this, it does maintain state in this simple example so its not "pure" const getSuperHeroes = ( era, offset = 0 ) => async () => fetchSuperHero( era, offset++ ) const nextOldSchoolSuperHero = getSuperHeroes(OLD_SUPERHERO_COLLECTION) const foo = await nextOldSchoolSuperHero()
@glitch3dout
@glitch3dout 6 лет назад
First
@snø_music0
@snø_music0 6 лет назад
What does it mean when you put "*" after function? Like this function* stuffs(){}
@funfunfunction
@funfunfunction 6 лет назад
That is what makes the function a generator instead of a normal function. I.e thats the core magic of the video. :)
Далее
Async iterators (for await ... of) in JavaScript
13:48
Generators - Javascript In Depth
43:18
Просмотров 1,8 тыс.
Музыкальные пародии
00:28
Просмотров 22 тыс.
Using async generators to stream data in JavaScript
27:37
Для чего генераторы в JavaScript?
14:00
ES2015 Iterators and Generators - Dan Shappir
44:45
Просмотров 8 тыс.
map for async iterators in JavaScript
25:59
Просмотров 19 тыс.
ES6 #16 Генераторы (Generators)
11:41
Просмотров 11 тыс.
Let's code a neural network in plain JavaScript Part 1
23:07
ES6 Iterator & Generator Fundamentals
18:18
Просмотров 31 тыс.