UNPKG

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