Write simple asynchronous code with JavaScript generators

15 minutes

Generators are a useful tool for handling asynchrony in your JavaScript applications. They were added to JavaScript with ES2015. When applied correctly they allow us to write asynchronous code that looks synchronous.

They also turn one of JavaScripts core paradigms on its head. Contrary to normal functions, generator functions can be paused and resumed at any arbitrary point in time with the yield keyword.

This course will walk you through the basics of generators so you’ll be able to use them in your applications without ripping your hair out!

pro-course-rss-logo

PRO RSS Feed

Use JavaScript (ES6) generators to pause function execution

Send messages to and from JavaScript (ES6) generators

Error handling in JavaScript (ES6) generators

Iterate over JavaScript (ES6) generators

Delegate JavaScript (ES6) generator iteration control

Use JavaScript (ES6) generators with Promises to handle async flows

js tutorial about Use JavaScript (ES6) generators to pause function execution

Use JavaScript (ES6) generators to pause function execution

1:47 js

ES6 generators make it possible to pause and resume function execution at arbitrary points. This video will introduce you to the basics of generators with the yield keyword.

js tutorial about Send messages to and from JavaScript (ES6) generators

Send messages to and from JavaScript (ES6) generators

1:48 js

We can communicate between generators and their instances with strategically placed yield keywords. This video will show you how to make it happen based on a simple example.

js tutorial about Error handling in JavaScript (ES6) generators

Error handling in JavaScript (ES6) generators

2:16 js

One can throw an error in a generator from the outside with it.throw(). Since all the code inside of a generator is synchronous, we can use the standard try/catch method of handling errors!

js tutorial about Iterate over JavaScript (ES6) generators

Iterate over JavaScript (ES6) generators

1:01 js

Generators are iterables, meaning we can easily iterate over the yielded values with a for-of loop. This video demonstrates this based on a simple counter example.

js tutorial about Delegate JavaScript (ES6) generator iteration control

Delegate JavaScript (ES6) generator iteration control

1:48 js

We can execute generators from generators, and delegate the iteration control with the yield* keyword.

Yo dawg, I heard you like generators, so I put generators inside your generators.

js tutorial about Use JavaScript (ES6) generators with Promises to handle async flows

Use JavaScript (ES6) generators with Promises to handle async flows

6:52 js

With one tiny utility function we’ll unlock the full power of generators to make them work well with Promises and thus be the perfect weapon for asynchronous flows in our apps.

Presented by:

Max Stoiber

Hi, I'm Max! 👋

I'm an open source developer and love coding. I make some things you might have heard of, like styled-components, react-boilerplate, KeystoneJS and CarteBlanche.

I'm also a huge coffee geek, skiing instructor and traveller.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?