Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators

LearnCode.academy · Beginner ·🌐 Frontend Engineering ·12y ago

Key Takeaways

The video covers the basics and applications of Javascript Generators, an ES6 feature, and how they can simplify asynchronous programming in Javascript with the help of libraries.

Full Transcript

All right, we're going to be covering generators, which are one of the most exciting Yeah, I'm just going to be honest. They are hands down the most exciting and powerful feature of the next version of JavaScript, which is ES6 Harmony. You can start using it now using Google Traceur. I'm going to put the link or I guess the name right there. You can figure it out. It's so easy to use. Um but generators are hands down a game changer. I know that that's a big statement to say, but it is completely true. It took me a while to realize that they were a game changer cuz when you look at the specs of what a generator technically is, at first it doesn't really seem like that useful of a thing. Um and then you have a lightbulb moment and everything starts changing for you. So, I'm going to show you technically what a generator is, how to code with it, how to use it, and then what that lightbulb moment is and why it's going to change so much of the way JavaScript is programmed. So, here's a basic generator. Basically, the only difference is it's got the star, which is how you define it as a generator, and then you can yield things. You can set yield points. And basically, what a generator is, if you were to define it very simply, it's a possible function or an iterable function. You can start running the function, and then when you get to your yield, it will actually stop running the function. While JavaScript continues doing everything it's go it's doing, this function has paused. And yield sits between the value you yielded and what it's passing on to the variable. Basically, yield sits between here and what the rest of JavaScript is going to do when you continue your function. Cuz you know, JavaScript basically runs right to left, uh right to left, right to left. So, you yield one. Uh let's go ahead and move on to this example. Um I'm going to run my gen, which is the name of my function, and uh I store it as gen. When I run it, nothing really happens. It just gets it set up. Nothing is started firing at in my function until I run gen.next. And then it runs until the first value has been yielded. So, here you can see value one done false. Um and then it's going to wait at this yield point until I run gen.next again. And so then it's going to return value two. See, there you go. I yielded the the number two. And then next time it's going to yield the number three. And then the next time it's going to yield undefined uh because it ran the next the rest of the function and there's no more yields. And it's going to say done true. So, now I'm done. If I were to foolishly try to run next again, it would error because you can't call next on a closed generator. So, that's basically what it is. You can pause it and yield stuff. And doesn't seem very useful yet, does it? Um and there's actually a problem with my code here is this is going to console.log undefined undefined undefined undefined because these values weren't set to anything. Remember when I said yield sits between here and here? Well, basically whatever it's going to wait for something to be passed to the next gen.next function. So, right now I'm not passing in anything. So, I yielded one out, but when you resume to the function, you didn't give me anything to pass back in. So, one is now undefined. So, to make this code work, I'd have to yield one. So, I passed one out here. And then I'm passing one back in. Two So, now one does equal one and then two and then three. So, now it will console.log 1 2 3. Um and I could actually if I were to yield 4 5 A. So, basically ignore what you passed me. I'm just going to pass in random stuff. Then one is now going to equal four and two is going to equal five and three is going to equal A. Uh so, because that's what I passed into the next functions. So, that's a generator. Um seem pointless? It's not. Let's say, and here was my lightbulb moment, let's say there's smart code that wraps a generator. Let's say you pass a generator to a smarter function that looks for promises. If you don't know what promises are, you need to pause right now and watch a video that I'm going to put in the description, my previous video on JavaScript promises, um and then come back to here. So, let's say we had a smarter piece of code here, and you were to pass it a generator, say my gen, and it would run that and get it ready to run. Uh and then it would run gen next, and it would get that yielded value. If that yielded value was a promise, which this is a This is really dumb smart code. You wouldn't check this way, but you know, I'm just kind of saying, say yielded value had a then method, then it would wait for that yielded value, that promise to resolve, and it would pass that resolved promise into your gen next function. So, then you could write code that actually did something like this. So, you can actually run code that said API friends. So, run API friends, yield is going to see, oh, that's a promise. I'm actually going to wait till that promise resolves, and then I'm going to actually pass the resolved value of that promise back on to one. So, if API friends return something like uh John, you know, just I don't know. Let's just say it returned a string of John, then one would equal John whenever this promise resolved. Ah, super cool. So, now I can actually do something like this. Get friends, get profile, get tweets, and it's going to run this, wait for it to complete, pass it into here, and then run the next thing, and now my code looks like it's just normal synchronous code, but it's actually asynchronous code happening over the span of I don't know, 20 seconds, 20 hours. It doesn't really care. It's just waiting for all these things to fire back as successful. Oh my goodness, that's super cool. So, how do I actually get this smart wrapping code? There's multiple libraries out there right now that already provide it for you. Bluebird, co, Q. Um if you're working on the browser side, client side, I'm going to say go Bluebird. Uh if you're working on Node.js, the back side, I'd say go with co. Um Q is actually already provided in Angular, so if you're using Angular, Q is the promise library that's baked into that. Uh but you can do this. You can run promise if you're using Bluebird. Promise.co routine. So, here we go. Tweets equals yield get tweets, and then we're going to console log it. That's it. I got the tweets and I console logged them. Uh let's say I needed to do three things in order. I need to get the tweets, then I need to get the profile, then I need to get the friends. Well, here you go. It's going to wait for this on the yields, pass it in, then it's going to wait for this, yield, pass it in, going to wait for this, yield, pass it in. When all three are successful, it's going to fire them into here. Oh my goodness, that is so cool. Uh but you can do even cooler things. You can configure Bluebird uh to be able to yield objects of promises or arrays of promises. I have an array of a promises here. Uh so, I can do an object here. I can yield tweets and profile, and it's going to run them both simultaneously, wait for them both to succeed, um and then it's going to pass them both into data as tweets and profile, and then I can console log data.tweets, and I can console log data.profile. By the way, all this code I'm going to put into a gist and put that link in the description so you can access this code and see it. Um but you can also using um the new destructured syntax in ES6, since you're probably already using Traceur um Traceur, however the French would pronounce that word correctly, um you can use destructured syntax, which means you can go var a, b equals um 10, 11. And what that's going to do is variable a equals 10, variable b equals 11. CoffeeScript does that if you guys have used CoffeeScript. So I can say variable tweetsProfile equals yield and give it an array of these two guys. And then I can just console log tweets and profile. It is so cool. It is so useful. Um I hope you guys love generators as much as I do. I'm already using these things in production on several little projects and I'm so excited about them. I hope you have a great day and enjoy the rest of your JavaScript life.

Original Description

Javascript Generators are AWESOME. At first glance, they seem overly simple and not very useful, but with a little bit of library love, they turn your async javascript from awful to joyful. View the code here: https://gist.github.com/learncodeacademy/bf04432597334190bef4 If you don't know promises, watch this video: http://www.youtube.com/watch?v=obaSQBBWZLk&list=UUVTlvUkGslCV_h-nSAId8Sw Basically, ES6 generators give you the ability to write async code as if it were synchronous code. It literally looks like you're not even writing async code! Harmony generators are definitely, without a doubt, the most powerful upgrade that javascript is receiving with ES6 harmony. -~-~~-~~~-~~-~- Also watch: "Responsive Design Tutorial - Tips for making web sites look great on any device" https://www.youtube.com/watch?v=fgOO9YUFlGI -~-~~-~~~-~~-~-
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LearnCode.academy · LearnCode.academy · 46 of 60

1 Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
LearnCode.academy
2 Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
LearnCode.academy
3 How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
LearnCode.academy
4 Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
LearnCode.academy
5 Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
LearnCode.academy
6 CSS Tutorial - Web Development Tutorial for Beginners (#5)
CSS Tutorial - Web Development Tutorial for Beginners (#5)
LearnCode.academy
7 Node.js tutorial for beginners - an introduction to Node.js with Express.js
Node.js tutorial for beginners - an introduction to Node.js with Express.js
LearnCode.academy
8 Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
LearnCode.academy
9 Javascript Tutorial - Programming Tutorial for Beginners Pt 1
Javascript Tutorial - Programming Tutorial for Beginners Pt 1
LearnCode.academy
10 Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
LearnCode.academy
11 AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
LearnCode.academy
12 WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
LearnCode.academy
13 YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
LearnCode.academy
14 BOWER! - Streamline Web Workflow with Bower Package Manager
BOWER! - Streamline Web Workflow with Bower Package Manager
LearnCode.academy
15 Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
LearnCode.academy
16 GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
LearnCode.academy
17 GITHUB PULL REQUEST, Branching, Merging & Team Workflow
GITHUB PULL REQUEST, Branching, Merging & Team Workflow
LearnCode.academy
18 Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
LearnCode.academy
19 jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
LearnCode.academy
20 jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
LearnCode.academy
21 jQuery Tutorial #1 - jQuery Tutorial for Beginners
jQuery Tutorial #1 - jQuery Tutorial for Beginners
LearnCode.academy
22 Node.js MongoDB Tutorial using Mongoose
Node.js MongoDB Tutorial using Mongoose
LearnCode.academy
23 Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
LearnCode.academy
24 WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
LearnCode.academy
25 jQuery Tutorial #4 - DOM Traversal with jQuery
jQuery Tutorial #4 - DOM Traversal with jQuery
LearnCode.academy
26 jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
LearnCode.academy
27 jQuery Tutorial #6 - Building a jQuery Image Slider
jQuery Tutorial #6 - Building a jQuery Image Slider
LearnCode.academy
28 jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
LearnCode.academy
29 jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
LearnCode.academy
30 jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
31 jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
32 How to put your website online - how to FTP to a domain & upload files to a webhost
How to put your website online - how to FTP to a domain & upload files to a webhost
LearnCode.academy
33 Basic Terminal Usage - Cheat Sheet to make the command line EASY
Basic Terminal Usage - Cheat Sheet to make the command line EASY
LearnCode.academy
34 SSH Tutorial - Basic server administration with SSH
SSH Tutorial - Basic server administration with SSH
LearnCode.academy
35 Vagrant Tutorial - Running a VM For Your Local Development Environment
Vagrant Tutorial - Running a VM For Your Local Development Environment
LearnCode.academy
36 Sublime Text Favorite Packages and Workflow
Sublime Text Favorite Packages and Workflow
LearnCode.academy
37 What Makes Javascript Weird...and AWESOME - Pt 1
What Makes Javascript Weird...and AWESOME - Pt 1
LearnCode.academy
38 Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
LearnCode.academy
39 Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
LearnCode.academy
40 FREE REST API - Practice Developing Javascript AJAX Apps with this API
FREE REST API - Practice Developing Javascript AJAX Apps with this API
LearnCode.academy
41 Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
LearnCode.academy
42 Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
LearnCode.academy
43 Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
LearnCode.academy
44 Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
LearnCode.academy
45 Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
LearnCode.academy
Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
LearnCode.academy
47 Web Development Advice - Interview with Dev Tips
Web Development Advice - Interview with Dev Tips
LearnCode.academy
48 How the Internet Works for Developers - Pt 2 - Servers & Scaling
How the Internet Works for Developers - Pt 2 - Servers & Scaling
LearnCode.academy
49 How the Internet Works for Developers - Pt 1 - Overview & Frontend
How the Internet Works for Developers - Pt 1 - Overview & Frontend
LearnCode.academy
50 HAPROXY vs NGINX - 10,000 requests while killing servers
HAPROXY vs NGINX - 10,000 requests while killing servers
LearnCode.academy
51 Node.js Cluster - Boost Node App Performance & Stability with Clustering
Node.js Cluster - Boost Node App Performance & Stability with Clustering
LearnCode.academy
52 Web Dev Training with Treehouse
Web Dev Training with Treehouse
LearnCode.academy
53 What is Node.js Exactly? - a beginners introduction to Nodejs
What is Node.js Exactly? - a beginners introduction to Nodejs
LearnCode.academy
54 How to deploy node.js applications #1 - spin up a server
How to deploy node.js applications #1 - spin up a server
LearnCode.academy
55 Deploying node.js applications #2 - provision server & setup flightplan
Deploying node.js applications #2 - provision server & setup flightplan
LearnCode.academy
56 Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
LearnCode.academy
57 Mobile Web Design - Coding Workflow For Mobile Websites
Mobile Web Design - Coding Workflow For Mobile Websites
LearnCode.academy
58 WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
LearnCode.academy
59 GRUNT TUTORIAL - Grunt makes your web development better!
GRUNT TUTORIAL - Grunt makes your web development better!
LearnCode.academy
60 STOP USING FTP!  - How to Deploy with Flightplan over SSH
STOP USING FTP! - How to Deploy with Flightplan over SSH
LearnCode.academy

The video teaches how to use Javascript Generators to simplify asynchronous programming in Javascript, making it more efficient and easier to manage. It covers the basics of Generators and how to use them with libraries to improve async code.

Key Takeaways
  1. Understand the basics of Javascript Generators
  2. Learn how to use Generators with libraries
  3. Apply Generators to simplify asynchronous programming
  4. Use async/await with Generators
  5. Practice using Generators in real-world scenarios
💡 Javascript Generators can greatly simplify asynchronous programming in Javascript when used with libraries, making it more efficient and easier to manage.

Related Reads

📰
HTML, CSS, and JavaScript Roadmap: Where Every Developer Actually Starts
Learn the fundamentals of HTML, CSS, and JavaScript before moving to frameworks like React to build a strong foundation for a career in web development
Medium · JavaScript
📰
Your React App Works. Until the API Doesn’t.
Learn to handle API failures in React apps to ensure a seamless user experience
Medium · JavaScript
📰
We Hit the Browser's Scroll Limit at 550K Rows. Here's How We Reached 1 Million.
Learn how to overcome browser scroll limits when rendering large datasets in React, a crucial skill for frontend engineers
Dev.to · JANG KIYOUNG
📰
I accidentally reinvented CSS progress() and then I published it anyway.
Learn how a developer accidentally recreated CSS progress() and what you can learn from their experience in CSS framework development
Dev.to · Petros Papatheodorou
Up next
Adapting to dynamic content using modern CSS
Kevin Powell
Watch →