-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
05-generators-yield.js
39 lines (29 loc) · 1.02 KB
/
05-generators-yield.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var puts = require('./lib/puts')
function * countDown (fuelStatus) {
if (fuelStatus.everythingGood) {
var engineStatus = yield 3 // engineStatus === {everythingGood: true}
}
if (engineStatus.everythingGood) {
var electricalStatus = yield 2
}
if (electricalStatus.everythingGood) {
var finalStatus = yield 1
}
if (finalStatus.everythingGood) {
return 0
}
}
var fuel = {everythingGood: true}
var iterator = countDown(fuel)
puts('starting countdown..')
var yieldedResult = iterator.next() // { value: 3, done: false }
puts(yieldedResult.value)
var engineCheck = {everythingGood: true}
yieldedResult = iterator.next(engineCheck) // { value: 2, done: false }
puts(yieldedResult.value)
var electricalCheck = {everythingGood: true}
yieldedResult = iterator.next(electricalCheck) // { value: 1, done: false }
puts(yieldedResult.value)
var finalCheck = {everythingGood: true}
yieldedResult = iterator.next(finalCheck) // { value: 0, done: true }
puts(yieldedResult.value)