-
Notifications
You must be signed in to change notification settings - Fork 792
/
generators.js
46 lines (39 loc) · 952 Bytes
/
generators.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
40
41
42
43
44
45
46
var assert = require('assert');
var co = require('..');
function sleep(ms) {
return function(done){
setTimeout(done, ms);
}
}
function *work() {
yield sleep(50);
return 'yay';
}
describe('co(*)', function(){
describe('with a generator function', function(){
it('should wrap with co()', function(){
return co(function *(){
var a = yield work;
var b = yield work;
var c = yield work;
assert('yay' == a);
assert('yay' == b);
assert('yay' == c);
var res = yield [work, work, work];
assert.deepEqual(['yay', 'yay', 'yay'], res);
});
})
it('should catch errors', function(){
return co(function *(){
yield function *(){
throw new Error('boom');
};
}).then(function () {
throw new Error('wtf')
}, function (err) {
assert(err);
assert(err.message == 'boom');
});
})
})
})