UNPKG

1.12 kBJavaScriptView Raw
1
2var co = require('..');
3var assert = require('assert');
4var Q = require('q');
5
6function getPromise(val, err) {
7 return Q.fcall(function(){
8 if (err) throw err;
9 return val;
10 });
11}
12
13describe('co(fn)', function(){
14 describe('with one promise yield', function(){
15 it('should work', function(done){
16 co(function *(){
17 var a = yield getPromise(1);
18 a.should.equal(1);
19 })(done);
20 })
21 })
22
23 describe('with several promise yields', function(){
24 it('should work', function(done){
25 co(function *(){
26 var a = yield getPromise(1);
27 var b = yield getPromise(2);
28 var c = yield getPromise(3);
29
30 [a,b,c].should.eql([1,2,3]);
31 })(done);
32 })
33 })
34
35 describe('when a promise is rejected', function(){
36 it('should throw and resume', function(done){
37 var error;
38
39 co(function *(){
40 try {
41 yield getPromise(1, new Error('boom'));
42 } catch (err) {
43 error = err;
44 }
45
46 assert('boom' == error.message);
47 var ret = yield getPromise(1);
48 assert(1 == ret);
49 })(done);
50 })
51 })
52})