Async iterators (for await ... of) in JavaScript

  Рет қаралды 37,813

Fun Fun Function

Fun Fun Function

Күн бұрын

Пікірлер: 122
@ErnestGWilsonII
@ErnestGWilsonII 6 жыл бұрын
Already subscribed with notifications enabled and thumbs up as always! I think this may have broken my brain! He did say put this into PROD right away correct? (kidding) - Beyond the mechanics and the fake database, I'd like to hear real world or additional hypothetical but desired use cases. Is the point to block the event loop less?
@funfunfunction
@funfunfunction 6 жыл бұрын
The benefit of having a function return an async iterator instead of just a promise that returns an array is that you can process it as you go. Imagine that you had a pretty big dataset (20GB) or perhaps that it was loading fairly slowly and you wanted to display the results as you were loading rather than wait for the entire dataset to load.
@ErnestGWilsonII
@ErnestGWilsonII 6 жыл бұрын
Now that is a use case that could really help! I will be keeping this in my back pocket for just such a rainy day! Thank you!
@snabelaset2
@snabelaset2 6 жыл бұрын
Just what I was wondering too, this example cleared it up, thank you.
@MaxDavydov-r2k
@MaxDavydov-r2k 6 жыл бұрын
By the way, all readable streams in Node.js 10 are async iterators! seetwitter.com/mikeal/status/993925507967205378
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 6 жыл бұрын
await for of is also supported as of version 10.3 node.green/#ES2018-features-Asynchronous-Iterators-for-await-of-loops
@JavierPortillo1
@JavierPortillo1 2 жыл бұрын
If you come from the future, for await is already supported by node and chrome. You also have to use Symbol.asyncIterator instead of Symbol.iterator.
@alexandergracilla1394
@alexandergracilla1394 Жыл бұрын
I used the Symbol.iterator and my program goes to an infinite loop and it took me a while to figure out why since the browser keep crashing
@TheSkoranoskorano
@TheSkoranoskorano 6 жыл бұрын
Awesome! ASYNC AUDIO :)
@funfunfunction
@funfunfunction 6 жыл бұрын
Hahahhahahha I love you
@backo5004
@backo5004 2 жыл бұрын
thank you! your videos helped me understand generators and async generators. i love your vibe (and sunglasses)!
@HcmfWice
@HcmfWice 6 жыл бұрын
I don't get it. I was trying to do something like this, and the 'for await .. of' just went into an infinite loop (basically the next() function is being called even after it had already returned a {done: true} value. So then I just copied your code from Github, and it also goes into an infinite loop. I have debugged it and the customer value in the body of the 'for await' loop is always undefined. On the other hand, if I do the (await iterator.next()).value thing, then it works correctly. What's going on?
@HcmfWice
@HcmfWice 6 жыл бұрын
I replaced Symbol.iterator with Symbol asyncIterator and now it works... Strange. I mean it's strange that it works for you with Symbol.iterator.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 6 жыл бұрын
+1 for the Gretzky reference!
@arminrosic
@arminrosic 4 жыл бұрын
+1 for supporting a fellow KZbinr ! I love your awesome coding content even more now Steve !!
@vuufke4327
@vuufke4327 2 жыл бұрын
I'm gonna miss this channel
@moy2010
@moy2010 6 жыл бұрын
Gooood monday morning! Finally the Async iterators' video :D. Thanks and kudos, MPJ!
@The_Nova_Glow
@The_Nova_Glow 6 жыл бұрын
I Love your intro. IT NEVER gets old.
@lukejagodzinski
@lukejagodzinski 6 жыл бұрын
It probably doesn't change anything about how code works but you should use Symbol.asyncIterator instead of Symbol.iterator to be compliant with the spec, right? Is there any difference between both besides just naming convention?
@funfunfunction
@funfunfunction 6 жыл бұрын
I think it's supposed to be asyncIterator, but I had to use the normal iterator in the code to make it work. I think that the Babel plugin I use is not quite interpreting the spec correctly.
@lukejagodzinski
@lukejagodzinski 6 жыл бұрын
Ok thanks :)
@stephanhouben6973
@stephanhouben6973 6 жыл бұрын
Node v10.11. supports async iterators natively and it requires the use of Symbol.asyncIterator. Which also conforms to the spec. Chrome 69 too. So perhaps this could be mentioned in the description.
@ratias0
@ratias0 5 жыл бұрын
I couldn't make your async example work in repl.it until I replaced [Symbol.iterator] with [Symbol.asyncIterator]
@FrancoisLecroart
@FrancoisLecroart 5 жыл бұрын
I didn't understand why it works on the video. I thought that AsynIterator must use Symbol.AsyncIterator to work with for await of.
@erikw4193
@erikw4193 5 жыл бұрын
@@FrancoisLecroart I think it's working in the video because of the babel transpilation. Symbol.asyncIterator is now native javascript as specified in ECMA-262, 9th edition, back in June 2018, one month after this video was released
@giovannicruz7023
@giovannicruz7023 6 жыл бұрын
Great video! What is your font family ?
@artgreg2296
@artgreg2296 6 жыл бұрын
Thanks for this one mpj, i think many people was expecting this synthesis
@marslogics
@marslogics 4 жыл бұрын
Loved it. From future when deno is just releases
@mokshchadha9151
@mokshchadha9151 4 жыл бұрын
I miss this dude
@8o8inSquares
@8o8inSquares 6 жыл бұрын
I like your hairstyle
@vcothur7
@vcothur7 6 жыл бұрын
3:37 what music is this!?
@searaig
@searaig 2 жыл бұрын
super helpful! thanks!
@theartist8835
@theartist8835 4 жыл бұрын
what plugin are you using to have results displayed next to your expressions ?
@ledz3pp
@ledz3pp 6 жыл бұрын
The audio seems to be out of sync at times
@funfunfunction
@funfunfunction 6 жыл бұрын
Yeah, I've been having som intermittent troubles with this recently. I have not yet been able to identify the source, sorry.
@egilsandnes9637
@egilsandnes9637 6 жыл бұрын
But is the async audio function iterable?
@kmbd1234
@kmbd1234 6 жыл бұрын
it's just AsyncAudio();
@DjalmaAraujo
@DjalmaAraujo 6 жыл бұрын
Nice video, as usual ;) Hey, what tool is that you use to output the result of the execution with //? Thanks
@MaxJacobson1
@MaxJacobson1 6 жыл бұрын
Djalma Araujo Looking for this as well
@stephenjames2951
@stephenjames2951 6 жыл бұрын
Quokka I believe.
@jinsyoommen
@jinsyoommen 6 жыл бұрын
Curious what the semicolon in front of the IIFE does..
@funfunfunction
@funfunfunction 6 жыл бұрын
When you're going semicolon-less and you're starting a line with a [, ( or a ` you should always prepend it with a ; because otherwise the interpreter might otherwise interpret it as calling the previous line as a function. Contrary to popular belief, this is the ONLY gotcha of the semicolon-less style. There is a rule in eslint (beforeStatementContinuationChars) that checks this for you so that you don't have to remember it.
@vidhill
@vidhill 6 жыл бұрын
He never uses semicolons at the end of his lines, IIFEs are one of those cases where not using semicolons can cause weird behaviour, so it's there, -just in case
@dusnoki
@dusnoki 6 жыл бұрын
Interesting stuff. Gonna see if I can use this but it looks a lot like Promise.all(), not sure what the benefits are at the moment.
@funfunfunction
@funfunfunction 6 жыл бұрын
Great question - what you're asking is - why just not return a promise that returns an array with the complete dataset? Lazy processing. What you need to imagine is a larger dataset / and or a slower access point. I.e. it takes a little while to load each item, or the data set is so large (or infinite!) that it's impractical or impossible to load it all into memory before starting processing. The advantage than an iterator gives over an array is communication back and forth between the consumer and the producer - the consumer can ask for more data gradually instead of waiting for it all at the same time.
@dusnoki
@dusnoki 6 жыл бұрын
Thanks for the detailed reply man :) Will keep this in mind if I stumble upon a use case like this. Keep up the great work, Cheers :)
@Alessandro-nq3tm
@Alessandro-nq3tm 6 жыл бұрын
The only advantage that I can imagine to use iterators over simple loops is when you must stop the iteration and resume it after using .next() OR if you have a reeeeeeaally large dataset
@igortobert7015
@igortobert7015 6 жыл бұрын
hey! whats'up! tell me what ide you are using that it gives such hints at the end of the line? much thnx. seems like VS Code, but what a package do you using?
@funfunfunction
@funfunfunction 6 жыл бұрын
Its quokka - quokka.funfunfunction.com
@cesarcastano
@cesarcastano 3 жыл бұрын
Very fun und helpful. Please use dark mode. Can't read with white backgrounds
@jhuluan-jyun2594
@jhuluan-jyun2594 5 жыл бұрын
Hello MPJ I have a question, I want to run the async code concurrently, so I wrap it in Array.from like this Array.from(customers), since it is a promise array, should be able to use Promise.all fetching data concurrently, but it somehow breaks! By the way, Awesome video as always:D
@funfunfunction
@funfunfunction 5 жыл бұрын
You cannot run async code synchronously - the point of providing an async interface is because it happens asynchronously.
@Eupolemos
@Eupolemos 6 жыл бұрын
The "mellow" mood didn't do anything negative for the vid :)
@tadeutupinamba9417
@tadeutupinamba9417 6 жыл бұрын
What the name of the plugin that returns instantelly the result of the line?
@kapros_p
@kapros_p 6 жыл бұрын
I think this is it: quokkajs.com/
@zf0666
@zf0666 6 жыл бұрын
What's the point of the the async iterator if you always await it anyway?
@funfunfunction
@funfunfunction 6 жыл бұрын
I dont understand your question. The whole point of asyncronous programming is so that we can await it.
@zf0666
@zf0666 6 жыл бұрын
I thought await is to make asynchronous code behave like synchronous code. If we wait for each get to finish it's 500 delay, what is the advantage of making the iterator async in the first place? Maybe I don't understand, but if we use await every time we call an asynchronous function isn't it the same as using synchronous code?
@funfunfunction
@funfunfunction 6 жыл бұрын
No, if we wanted syncronous code we’d just make the code syncronous in the first place, not make an async api and then use a tool to make it sync again. Syncronous code freezes your application completely allowing no other execution of any code whatsoever until something has completed. This is one of the biggest misunderstandings of the async keyword, if it made code sync it would have been named sync, not async. When we await inside an async function, the async function is paused and other code is allowed to execute while the async function is awaiting an operation. It *visually flows* like syncronous code, but it doesn’t turn code sync, because that would be stupid. Async APIs are async for a reason - if they were suitable to be sync they would be sync.
@morleee
@morleee 6 жыл бұрын
Hi MPJ, when I run your example with the for await loop, it enters into an infinite loop. I was under the impression that the for await loop would be able to log the result of the promise on every iteration but it doesn't seem like it does.
@morleee
@morleee 6 жыл бұрын
I happened to watch the async generator video after this, and I saw Symbol.asyncIterator was used. When switching to an asyncIterator for this example, the for await loop works as expected. Though i'm confused as to how it works in your example in the video (using just Symbol.iterator).... I'm on Node 10.3.0 FYI
@funfunfunction
@funfunfunction 6 жыл бұрын
I think it's because Quokka runs babel on the code, and I thiiink babel has some weird behaviour with asyncIterator vs just Iterator.
@morleee
@morleee 6 жыл бұрын
Thanks for the response! Great videos by the way :)
@mattd6908
@mattd6908 6 жыл бұрын
Great episode as always, have you thought about doing an ep or two on RxJS? I know you have briefly covered observables and don't typically go down the path of libraries / frameworks but being that a lot of your content is based on async, I think it would be valuable to a lot of people. It's an intimidating library that I think people don't even give a chance due to that.
@funfunfunction
@funfunfunction 6 жыл бұрын
I’m honestly an anti-fan of Rx to be honest, and don’t want to push it. I think its an overrated library that makes a simple concept such as observables super unapproachable and hard to deal with. It’s intimidating because it’s grown indiscriminately and I think people are perfectly right to be afraid of it. I also don’t subscribe to their philosophy of sprinkling observables all over the codebase - the observable pattern should be used with great restraint.
@mattd6908
@mattd6908 6 жыл бұрын
Fair enough, appreciate you taking the time to reply. Keep up the great work!
@kwabenaboadu
@kwabenaboadu 6 жыл бұрын
Hey MPJ. Nice video. Just a quick suggestion. Node has util.promisify which can be used to wrap setTimeout in a promise. Thanks
@funfunfunction
@funfunfunction 6 жыл бұрын
Yeah, but this way, the viewer will understand what is happening and won’t be distracted. Especially for the case of setTimeout, where util.promisify has a special workaround to handle the unorthodox call signature of setTimeout, which is doubly surprising.
@kwabenaboadu
@kwabenaboadu 6 жыл бұрын
Fun Fun Function Again thanks for the videos.
@MuratKeremOzcan
@MuratKeremOzcan 6 жыл бұрын
Could you do this with forEach ?
@funfunfunction
@funfunfunction 6 жыл бұрын
You need to elaborate this question a bit because it doesn't make sense for me. forEach is a method on an array, not sure what you mean. Did you watch the first video?
@MuratKeremOzcan
@MuratKeremOzcan 6 жыл бұрын
Fun Fun Function can you asynchronously iterate over an array of promises using forEach, instead of for of? What is the difference?
@funfunfunction
@funfunfunction 6 жыл бұрын
No, forEach, unlike (for await ... of) is sync. Also, forEach is a method exclusive to arrays. Iterators is a general interface that arrays happens to use, you might have streams that are iterable, for example.
@MuratKeremOzcan
@MuratKeremOzcan 6 жыл бұрын
Fun Fun Function thank you, you saved me a few days
@Cazineer
@Cazineer 6 жыл бұрын
Fun Fun Function Maps and Sets have forEach as well, so no, forEach is not exclusive to arrays.
@Joshfw27492
@Joshfw27492 6 жыл бұрын
There's a library called interactive extensions for javascript which has async iteration baked in. I'd imagine this is the dual cousin of RXJS but instead of modelling push based data sources it models conventional pull based data sources. I'm yet to see strong documentation or any examples elsewhere on google. github.com/reactivex/ixjs
@ktquez
@ktquez 6 жыл бұрын
What screen recording software does this effect on the mouse, does anyone know?
@funfunfunction
@funfunfunction 6 жыл бұрын
See behind the scenes and gear video
@alanlal5625
@alanlal5625 6 жыл бұрын
You have so many cool glasses ❤️❤️... Would love to see your collection in your video some day..
@asimsinthehouse
@asimsinthehouse 6 жыл бұрын
Great video as always! For the next video, what about Async Generators! That would be interesting
@ABHISHEK0058
@ABHISHEK0058 6 жыл бұрын
Thanks man .. just saw two previous videos .. and now this ... got lucky i guess
@andreyshedko6155
@andreyshedko6155 6 жыл бұрын
Hi MPJ! Why module.export? What's wrong with import and export? P.S. I love your videos!
@funfunfunction
@funfunfunction 6 жыл бұрын
They are still ultra buggy in node, at least when I tried a month ago or so.
@putinninovacuna8976
@putinninovacuna8976 5 жыл бұрын
It's kinda like streambuilder in Flutter with the iterator
@Higoki1
@Higoki1 6 жыл бұрын
Awesome intro. Very funny.
@spacewad8745
@spacewad8745 6 жыл бұрын
I see you've switched to a lighter text editor theme. Is it good for your eyes? I am really curious.
@funfunfunction
@funfunfunction 6 жыл бұрын
All research I've seen suggests that dark text on bright background is easier to read for humans. That said, many programmers anecdotally report that they have less strain with bright on dark. Not sure why this is - it might be because programmers often have astigmatism which makes bright on dark easier to read. It might also be because programmers work in insufficient lighting conditions which make a bright screen straining due to contrast between environment and screen.
@mdoerkse
@mdoerkse 4 жыл бұрын
Is it the future yet?
@ZiX2345
@ZiX2345 6 жыл бұрын
Hi MPJ. Nice hair. Good explanation, thanks!
@serafim2867
@serafim2867 6 жыл бұрын
Hi, pls send music from this video to some cloud, I really get tired trying to find it =)
@brisketbaron
@brisketbaron 6 жыл бұрын
I love it when you parrot the opinions of the usual suspects. You need more props though. hats!
@amarbalu109
@amarbalu109 4 жыл бұрын
Awesome.
@jsonkody
@jsonkody 6 жыл бұрын
Thats exactly how I feel now after yesterday trip on top of nearby mountain ;)
@afiefsholichuddin
@afiefsholichuddin 6 жыл бұрын
Did you forget to use generator function? :D
@funfunfunction
@funfunfunction 6 жыл бұрын
No, we’re not there yet. We need to understand async iterators before we introduce things that meta-programs them.
@MrAidooyaw
@MrAidooyaw 4 жыл бұрын
U sound like Cpt Jack Sparrow. I like that 😊
@chanlito_
@chanlito_ 6 жыл бұрын
😱that white background... Pls use dark
@funfunfunction
@funfunfunction 6 жыл бұрын
You sure do know how to make a strong case when arguing.
@ph10m
@ph10m 6 жыл бұрын
I prefer green with pink text * emoji goes here *
@noxabellus
@noxabellus 6 жыл бұрын
Up until you started actually typing it, I was like "Wait, WTF is Bauble?"
@funfunfunction
@funfunfunction 6 жыл бұрын
Oh, thats interesting. Can I ask what your background is?
@javierRC82857
@javierRC82857 6 жыл бұрын
Why is not high contrast? Why are the letters small? Please. I normally see your videos in my phone.
@funfunfunction
@funfunfunction 6 жыл бұрын
Dark text on white background is the highest contrast you can get (black on yellow being the highest). Sorry about the small letters though, I forgot myself this time.
@javierRC82857
@javierRC82857 6 жыл бұрын
Fun Fun Function Yes. I was referring to the dark theme of black background and yellow letters. Thanks, very good videos.
@funfunfunction
@funfunfunction 6 жыл бұрын
You misunderstand. The highest possible contrast of colors that you can achieve is dark letters on yellow background. The inverse is pretty bad. www.viget.com/articles/color-contrast/
@igortobert7015
@igortobert7015 6 жыл бұрын
async function (sound, video) :D PS video in frame not sync with sound and desktop video
@maxlin2905
@maxlin2905 4 жыл бұрын
Lucky! I am from the ”future”
@greendsnow
@greendsnow 2 жыл бұрын
what's he talking about?
@yashkalwani2674
@yashkalwani2674 6 жыл бұрын
The pain what you did to so much of caffeine and such a beautiful coffee filter made me pause the video come to comments section and make this comment. Please, do not do that again. Its painful.
@johnfridja
@johnfridja 4 жыл бұрын
i pressed like just for the silly intro
@f1945
@f1945 6 жыл бұрын
you look like german garmendia.
@anison1111
@anison1111 3 жыл бұрын
you should do TV shows not programming
@keologii
@keologii Жыл бұрын
Please remove the music from all of your videos
Using async generators to stream data in JavaScript
27:37
Fun Fun Function
Рет қаралды 35 М.
map for async iterators in JavaScript
25:59
Fun Fun Function
Рет қаралды 19 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
Iterators in JavaScript using Quokka.js
21:07
Fun Fun Function
Рет қаралды 111 М.
Generators in JavaScript
22:16
Fun Fun Function
Рет қаралды 49 М.
The Power of JS Generators by Anjana Vakil
36:10
JSConf
Рет қаралды 169 М.
C# Async/Await/Task Explained (Deep Dive)
24:22
Raw Coding
Рет қаралды 421 М.
Higher-order iterators in JavaScript
14:00
Fun Fun Function
Рет қаралды 17 М.
Welcome to Async Generators in JavaScript
10:53
Andrew Burgess
Рет қаралды 6 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 687 М.
What are JavaScript Generators and Iterators?
10:28
Andrew Burgess
Рет қаралды 7 М.
Tips For Using Async/Await in JavaScript
16:26
James Q Quick
Рет қаралды 396 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН