Table of Contents
Preface v
1 Asynchronous JavaScript 1
Callbacks 2
Asynchronous JavaScript 3
Run to Completion and the Event Loop 6
Summary 10
2 Introducing Promises 11
Basic Usage 11
Multiple Consumers 14
Promise States 15
Chaining Promises 18
Callback Execution Order 19
Basic Error Propagation 20
The Promise API 22
Summary 24
3 Working with Standard Promises 25
The Async Ripple Effect 25
Conditional Logic 26
Parallel Execution 28
Sequential Execution Using Loops or Recursion 30
Managing Latency 35
Functional Composition 36
Summary 37
4 Using Libraries and Frameworks 39
Promise Interoperability and Thenables 40
The Bluebird Promise Library 40
Loading Bluebird 41
Managing Execution Context 41
Wrapping Node.js Functions 43
Working with Collections of Promises 46
Manipulating Fulfillment Values 48
Promises in jQuery 50
Summary 54
5 Error Handling 55
Rejecting Promises 55
Passing Errors 57
Unhandled Rejections 58
Implementing try/catch/finally 59
Using the Call Stack 61
Summary 63
6 Combining ECMAScript 6 Features with Promises 65
Destructuring 65
Arrow Functions 67
Iterables and Iterators 68
Generators 69
Synchronous Style 69
Generators and Iterators 70
Sending Values to a Generator 72
Sending Errors to a Generator 76
Practical Application 77
Summary 79
Index 81