UNPKG

854 BJavaScriptView Raw
1
2var request = require('request');
3var thunk = require('thunkify');
4var co = require('..');
5var join = co.join;
6
7var get = thunk(request.get);
8
9// measure response time N times
10
11function *latency(url, times) {
12 var ret = [];
13
14 while (times--) {
15 var start = new Date;
16 yield get(url);
17 ret.push(new Date - start);
18 }
19
20 return ret;
21}
22
23// run each test in sequence
24
25co(function *(){
26 var a = yield latency('http://google.com', 5);
27 console.log(a);
28
29 var b = yield latency('http://yahoo.com', 5);
30 console.log(b);
31
32 var c = yield latency('http://cloudup.com', 5);
33 console.log(c);
34})()
35
36// run each test in parallel, order is retained
37
38co(function *(){
39 var a = latency('http://google.com', 5);
40 var b = latency('http://yahoo.com', 5);
41 var c = latency('http://cloudup.com', 5);
42
43 var res = yield [a, b, c];
44 console.log(res);
45})()
46