LEARNING JAVASCRIPT - Trang 205

the value that was passed in by

next

. Here’s what it looks like when we run this gener‐

ator through to completion:

const

it

=

interrogate

();

it

.

next

();

// { value: "What is your name?", done: false }

it

.

next

(

'Ethan'

);

// { value: "What is your favorite color?", done: false }

it

.

next

(

'orange'

);

// { value: "Ethan's favorite color is orange.", done: true }

Figure 12-1

shows the sequence of events as this generator is run.

Figure 12-1. Generator example

This example demonstrates that generators are quite powerful, allowing the execution

of functions to be controlled by the caller. Also, because the caller can pass informa‐

tion into the generator, the generator can even modify its own behavior based on

what information is passed in.

You can’t create a generator with arrow notation; you have to use
function*

.

Generators | 181

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.