1
1
exports . install = function ( ) {
2
- F . route ( '/' , view_index ) ;
2
+ F . route ( '/' , json_index ) ;
3
+
4
+ // Recommend:
5
+ F . route ( '/array/' , json_array ) ;
6
+ F . route ( '/array2/' , json_array2 ) ;
7
+ F . route ( '/number/' , json_number ) ;
3
8
} ;
4
9
5
- function view_index ( ) {
10
+ function json_index ( ) {
6
11
7
12
var self = this ;
8
- var builder = [ ] ;
9
- var async = new Utils . Async ( ) ;
13
+ var response = [ ] ;
14
+ var async = new U . Async ( ) ;
10
15
11
16
async . await ( function ( complete ) {
12
- utils . request ( 'https://www.google.com' , [ 'get' ] , null , function ( err , data ) {
17
+ U . request ( 'https://www.google.com' , [ 'get' ] , function ( err , data ) {
13
18
var output = err ? 'error' : data . length . toString ( ) ;
14
- builder . push ( 'www.google.com -> ' + output ) ;
19
+ response . push ( 'www.google.com -> ' + output ) ;
15
20
complete ( ) ;
16
21
} ) ;
17
22
} ) ;
18
23
19
24
async . await ( function ( complete ) {
20
- utils . request ( 'http://www.expressjs.com' , [ 'get' ] , null , function ( err , data ) {
25
+ U . request ( 'http://www.expressjs.com' , [ 'get' ] , function ( err , data ) {
21
26
var output = err ? 'error' : data . length . toString ( ) ;
22
- builder . push ( 'www.expressjs.com -> ' + output ) ;
27
+ response . push ( 'www.expressjs.com -> ' + output ) ;
23
28
complete ( ) ;
24
29
} ) ;
25
30
} ) ;
26
31
27
32
async . await ( function ( complete ) {
28
- utils . request ( 'http://www.yahoo.com' , [ 'get' ] , null , function ( err , data ) {
33
+ U . request ( 'http://www.yahoo.com' , [ 'get' ] , function ( err , data ) {
29
34
var output = err ? 'error' : data . length . toString ( ) ;
30
- builder . push ( 'www.yahoo.com -> ' + output ) ;
35
+ response . push ( 'www.yahoo.com -> ' + output ) ;
31
36
complete ( ) ;
32
37
} ) ;
33
38
} ) ;
34
39
35
40
async . await ( 'partial' , function ( complete ) {
36
- utils . request ( 'http://www.totaljs.com' , [ 'get' ] , null , function ( err , data ) {
41
+ U . request ( 'http://www.totaljs.com' , [ 'get' ] , function ( err , data ) {
37
42
var output = err ? 'error' : data . length . toString ( ) ;
38
- builder . push ( 'www.totaljs.com -> ' + output ) ;
43
+ response . push ( 'www.totaljs.com -> ' + output ) ;
39
44
complete ( ) ;
40
45
} ) ;
41
46
} ) ;
42
47
43
48
// waiting for await('partial')
44
49
async . wait ( 'waiting 1' , 'partial' , function ( complete ) {
45
50
console . log ( 'waiting 1 complete' ) ;
46
- setTimeout ( function ( ) {
47
- complete ( ) ;
48
- } , 1000 ) ;
51
+ setTimeout ( ( ) => complete ( ) , 1000 ) ;
49
52
} ) ;
50
53
51
54
// waiting for wait('waiting')
52
55
async . wait ( 'waiting 2' , 'waiting 1' , function ( complete ) {
53
56
console . log ( 'waiting 2 complete' ) ;
54
- setTimeout ( function ( ) {
55
- complete ( ) ;
56
- } , 1000 ) ;
57
+ setTimeout ( ( ) => complete ( ) , 1000 ) ;
57
58
} ) ;
58
59
59
60
async . run ( function ( ) {
60
61
if ( self . xhr )
61
- return self . json ( builder ) ;
62
- self . view ( 'index' , builder ) ;
62
+ return self . json ( response ) ;
63
+ self . view ( 'index' , response ) ;
64
+ } ) ;
65
+ }
66
+
67
+ function json_array ( ) {
68
+ var async = [ ] ;
69
+ var response = [ ] ;
70
+ var self = this ;
71
+
72
+ async . push ( function ( next ) {
73
+ U . request ( 'https://www.google.com' , [ 'get' ] , function ( err , data ) {
74
+ var output = err ? 'error' : data . length . toString ( ) ;
75
+ response . push ( 'www.google.com -> ' + output ) ;
76
+ next ( ) ;
77
+ } ) ;
78
+ } ) ;
79
+
80
+ async . push ( function ( next ) {
81
+ U . request ( 'http://www.expressjs.com' , [ 'get' ] , function ( err , data ) {
82
+ var output = err ? 'error' : data . length . toString ( ) ;
83
+ response . push ( 'www.expressjs.com -> ' + output ) ;
84
+ next ( ) ;
85
+ } ) ;
86
+ } ) ;
87
+
88
+ async . push ( function ( next ) {
89
+ U . request ( 'http://www.yahoo.com' , [ 'get' ] , function ( err , data ) {
90
+ var output = err ? 'error' : data . length . toString ( ) ;
91
+ response . push ( 'www.yahoo.com -> ' + output ) ;
92
+ next ( ) ;
93
+ } ) ;
63
94
} ) ;
95
+
96
+ async . async ( ( ) => self . json ( response ) ) ;
97
+ }
98
+
99
+ function json_array2 ( ) {
100
+ var url = [ 'https://www.google.com' , 'http://www.expressjs.com' , 'http://www.yahoo.com' ] ;
101
+ var response = [ ] ;
102
+ var self = this ;
103
+
104
+ url . wait ( function ( item , next ) {
105
+ U . request ( item , [ 'get' ] , function ( err , data ) {
106
+ var output = err ? 'error' : data . length . toString ( ) ;
107
+ response . push ( item + ' -> ' + output ) ;
108
+ next ( ) ;
109
+ } ) ;
110
+ } , ( ) => self . json ( response ) ) ;
111
+ }
112
+
113
+ function json_number ( ) {
114
+
115
+ var self = this ;
116
+ var response = [ ] ;
117
+ var url = [ 'https://www.google.com' , 'http://www.expressjs.com' , 'http://www.yahoo.com' ] ;
118
+ var count = url . length ;
119
+
120
+ count . async ( function ( index , next ) {
121
+
122
+ index -= 1 ;
123
+
124
+ U . request ( url [ index ] , [ 'get' ] , function ( err , data ) {
125
+ var output = err ? 'error' : data . length . toString ( ) ;
126
+ response . push ( url [ index ] + ' -> ' + output ) ;
127
+ next ( ) ;
128
+ } ) ;
129
+
130
+ } , ( ) => self . json ( response ) ) ;
64
131
}
0 commit comments