File tree Expand file tree Collapse file tree 4 files changed +37
-35
lines changed Expand file tree Collapse file tree 4 files changed +37
-35
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
function * sleep ( interval ) {
4
- let start = new Date ( ) ;
4
+ const start = new Date ( ) ;
5
5
while ( new Date ( ) - start < interval ) {
6
6
yield ;
7
7
}
@@ -18,23 +18,25 @@ function* consume() {
18
18
let count = 0 ;
19
19
let sum = 0 ;
20
20
while ( true ) {
21
- let data = yield ;
21
+ const data = yield ;
22
22
count ++ ;
23
23
sum += data ;
24
- console . log ( `Got data: ${ data } \n` +
25
- `Count: ${ count } \n` +
26
- `Sum: ${ sum } \n` +
27
- `Average: ${ sum / count } \n` ) ;
24
+ console . log (
25
+ `Got data: ${ data } \n` +
26
+ `Count: ${ count } \n` +
27
+ `Sum: ${ sum } \n` +
28
+ `Average: ${ sum / count } \n`
29
+ ) ;
28
30
}
29
31
}
30
32
31
- let consumer = consume ( ) ;
32
- let producer = produce ( consumer ) ;
33
+ const consumer = consume ( ) ;
34
+ const producer = produce ( consumer ) ;
33
35
consumer . next ( ) ;
34
36
35
- function step ( ) {
37
+ const step = ( ) => {
36
38
producer . next ( ) ;
37
39
setImmediate ( step ) ;
38
- }
40
+ } ;
39
41
40
42
step ( ) ;
Original file line number Diff line number Diff line change @@ -7,9 +7,10 @@ const doMonad = require('./helpers/do-notation');
7
7
8
8
const baseUrl = 'http://localhost:3000/' ;
9
9
doMonad ( function * ( ) {
10
- let api = yield getJSON ( baseUrl ) ;
11
- for ( let resource of api . resources ) {
12
- let data = yield getJSON ( baseUrl + resource ) ;
10
+ const api = yield getJSON ( baseUrl ) ;
11
+ let resource , data ;
12
+ for ( resource of api . resources ) {
13
+ data = yield getJSON ( baseUrl + resource ) ;
13
14
console . log ( data ) ;
14
15
}
15
16
} ) ;
Original file line number Diff line number Diff line change @@ -7,9 +7,10 @@ const doMonad = require('./helpers/do-notation');
7
7
8
8
const baseUrl = 'http://localhost:3000/' ;
9
9
doMonad ( function * ( ) {
10
- let api = yield getJSON ( baseUrl ) ;
11
- for ( let resource of api . resources ) {
12
- let data = yield getJSON ( baseUrl + resource ) ;
10
+ const api = yield getJSON ( baseUrl ) ;
11
+ let resource , data ;
12
+ for ( resource of api . resources ) {
13
+ data = yield getJSON ( baseUrl + resource ) ;
13
14
console . log ( data ) ;
14
15
}
15
16
} ) . catch ( e => {
Original file line number Diff line number Diff line change 2
2
3
3
const doMonad = require ( './helpers/do-notation' ) ;
4
4
5
+ const Nothing = {
6
+ then ( ) {
7
+ return Nothing ;
8
+ } ,
9
+
10
+ catch ( callback ) {
11
+ callback ( ) ;
12
+ return this ;
13
+ }
14
+ } ;
15
+
5
16
class Just {
6
17
constructor ( value ) {
7
18
this . value = value ;
8
19
}
9
20
10
21
then ( fn ) {
11
- let result = fn ( this . value ) ;
12
- if ( result instanceof Just ||
13
- result === Nothing ) {
22
+ const result = fn ( this . value ) ;
23
+ if ( result instanceof Just || result === Nothing ) {
14
24
return result ;
15
- } else {
16
- return new Just ( result ) ;
17
25
}
26
+ return new Just ( result ) ;
18
27
}
19
28
20
29
catch ( ) {
21
30
return this ;
22
31
}
23
32
}
24
33
25
- const Nothing = {
26
- then ( ) {
27
- return Nothing ;
28
- } ,
29
-
30
- catch ( callback ) {
31
- callback ( ) ;
32
- return this ;
33
- }
34
- } ;
35
-
36
34
doMonad ( function * ( ) {
37
- let a = yield new Just ( 5 ) ;
38
- let b = yield new Just ( 3 ) ;
39
- let c = yield new Just ( 4 ) ;
35
+ const a = yield new Just ( 5 ) ;
36
+ const b = yield new Just ( 3 ) ;
37
+ const c = yield new Just ( 4 ) ;
40
38
console . log ( ( a + b ) * c ) ;
41
39
} ) . catch ( ( ) => {
42
40
console . log ( 'At least one value is not present' ) ;
You can’t perform that action at this time.
0 commit comments