Chapter 6. Functions for the future: generators and promises

 

This chapter covers

  • Continuing function execution with generators
  • Handling asynchronous tasks with promises
  • Achieving elegant asynchronous code by combining generators and promises

In the previous three chapters, we focused on functions, specifically on how to define functions and how to use them to great effect. Although we’ve introduced some ES6 features, such as arrow functions and block scopes, we’ve mostly been exploring features that have been part of JavaScript for quite some time. This chapter tackles the cutting edge of ES6 by presenting generators and promises, two completely new JavaScript features.

Note

Generators and promises are both introduced by ES6. You can check out current browser support at http://mng.bz/sOs4 and http://mng.bz/Du38.

Generators are a special type of function. Whereas a standard function produces at most a single value while running its code from start to finish, generators produce multiple values, on a per request basis, while suspending their execution between these requests. Although new in JavaScript, generators have existed for quite some time in Python, PHP, and C#.

Do you know?

6.1. Making our async code elegant with generators and promises

6.2. Working with generator functions

6.3. Working with promises

6.4. Combining generators and promises

6.5. Summary

6.6. Exercises

sitemap