Eloquent Javascript

Page 203

value as the response value, that would mean that a request handler can’t itself perform asynchronous actions. A function doing asynchronous work typically returns before the work is done, having arranged for a callback to be called when it completes. So we need some asynchronous mechanism—in this case, another callback function—to signal when a response is available. In a way, asynchronicity is contagious. Any function that calls a function that works asynchronously must itself be asynchronous, using a callback or similar mechanism to deliver its result. Calling a callback is somewhat more involved and error-prone than simply returning a value, so needing to structure large parts of your program that way is not great.

Promises Working with abstract concepts is often easier when those concepts can be represented by values. In the case of asynchronous actions, you could, instead of arranging for a function to be called at some point in the future, return an object that represents this future event. This is what the standard class Promise is for. A promise is an asynchronous action that may complete at some point and produce a value. It is able to notify anyone who is interested when its value is available. The easiest way to create a promise is by calling Promise.resolve. This function ensures that the value you give it is wrapped in a promise. If it’s already a promise, it is simply returned—otherwise, you get a new promise that immediately finishes with your value as its result. let fifteen = Promise.resolve(15); fifteen.then(value => console.log(`Got ${value}`)); // → Got 15

To get the result of a promise, you can use its then method. This registers a callback function to be called when the promise resolves and produces a value. You can add multiple callbacks to a single promise, and they will be called, even if you add them after the promise has already resolved (finished). But that’s not all the then method does. It returns another promise, which resolves to the value that the handler function returns or, if that returns a promise, waits for that promise and then resolves to its result. It is useful to think of promises as a device to move values into an asynchronous reality. A normal value is simply there. A promised value is a value

191


Turn static files into dynamic content formats.

Create a flipbook

Articles inside

Compatibility and the browser wars

0
page 231

In the sandbox

0
page 230

The standard

1min
page 234

HTML and JavaScript

1min
page 229

Exercises

4min
pages 221-223

Functions

3min
pages 218-219

The evaluator

1min
page 214

Asynchronous bugs

3min
pages 205-206

The environment

1min
page 217

Special forms

2min
pages 215-216

The event loop

1min
page 204

Generators

1min
page 203

Async functions

3min
pages 201-202

Message routing

4min
pages 198-200

Failure

3min
pages 192-193

Network flooding

1min
page 197

Networks are hard

4min
pages 194-195

Collections of promises

1min
page 196

Promises

1min
page 191

Callbacks

2min
pages 189-190

Crow tech

1min
page 188

Building and bundling

1min
page 181

ECMAScript modules

3min
pages 179-180

Improvised modules

1min
page 175

CommonJS

2min
pages 177-178

Evaluating data as code

1min
page 176

Packages

1min
page 174

Summary

3min
pages 169-170

International characters

1min
page 168

Parsing an INI file

2min
pages 166-167

Greed

1min
page 161

The lastIndex property

2min
pages 164-165

The replace method

1min
pages 159-160

Dynamically creating RegExp objects

1min
page 162

The search method

1min
page 163

Backtracking

2min
pages 157-158

Word and string boundaries

1min
page 155

The Date class

1min
page 154

Cleaning up after exceptions

3min
pages 141-142

Repeating parts of a pattern

1min
page 151

Exceptions

2min
pages 139-140

Assertions

1min
page 145

Selective catching

3min
pages 143-144

Error propagation

1min
page 138

Debugging

2min
pages 136-137

Testing

1min
page 135

Simulation

2min
pages 126-127

Types

0
page 134

Strict mode

0
page 133

Exercises

1min
page 131

Persistent data

1min
page 125

The task

2min
pages 123-124

Exercises

2min
pages 119-120

Summary

1min
page 118

Inheritance

1min
page 116

The iterator interface

2min
pages 112-113

Maps

2min
pages 108-109

Overriding derived properties

2min
pages 106-107

Class notation

1min
page 105

Classes

1min
page 104

Prototypes

2min
pages 102-103

Methods

1min
page 101

Exercises

0
page 99

Recognizing text

1min
page 97

Strings and character codes

2min
pages 95-96

Summary

1min
page 98

Composability

2min
pages 93-94

Summarizing with reduce

1min
page 92

Transforming with map

1min
page 91

Filtering arrays

1min
page 90

Exercises

3min
pages 82-84

Script data set

1min
page 89

5 Higher-Order Functions

1min
page 85

Higher-order functions

0
page 88

Summary

1min
page 81

JSON

1min
page 80

Destructuring

1min
page 79

The Math object

2min
pages 77-78

Rest parameters

1min
page 76

Strings and their properties

2min
pages 74-75

Further arrayology

1min
page 73

The final analysis

3min
pages 71-72

Array loops

0
page 70

Computing correlation

1min
page 69

The lycanthrope’s log

3min
pages 67-68

Mutability

2min
pages 65-66

Data sets

1min
page 60

Properties

1min
page 61

Objects

3min
pages 63-64

Methods

1min
page 62

Exercises

2min
pages 57-58

Summary

1min
page 56

Functions and side effects

1min
page 55

Growing functions

2min
pages 53-54

Recursion

3min
pages 50-52

Closure

1min
page 49

Optional Arguments

2min
pages 47-48

The call stack

1min
page 46

Arrow functions

1min
page 45

Declaration notation

1min
page 44

Functions as values

1min
page 43

Bindings and scopes

2min
pages 41-42

Exercises

2min
pages 38-39

Summary

1min
page 37

Dispatching on a value with switch

1min
page 35

Summary

0
page 21

Updating bindings succinctly

1min
page 34

Indenting Code

1min
page 32

while and do loops

2min
pages 30-31

Conditional execution

2min
pages 28-29

Strings

4min
pages 13-14

Bindings

3min
pages 23-24

Unary operators

1min
page 15
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.