1 | var chai = require('chai');
|
2 | var Promise = require('bluebird');
|
3 | var async = require('../src/async/index');
|
4 | var await = require('../src/await/index');
|
5 | var expect = chai.expect;
|
6 | describe('A suspendable function returned by async.cps(...)', function () {
|
7 | it('synchronously returns nothing', function () {
|
8 | var foo = async.cps(function () { });
|
9 | var syncResult = foo(function () { });
|
10 | expect(syncResult).to.not.exist;
|
11 | });
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | it('begins executing synchronously and completes asynchronously', function (done) {
|
22 | var x = 5;
|
23 | var foo = async.cps(function () { x = 7; });
|
24 | Promise.promisify(foo)()
|
25 | .then(function () { return expect(x).to.equal(9); })
|
26 | .then(function () { return done(); })
|
27 | .catch(done);
|
28 | expect(x).to.equal(7);
|
29 | x = 9;
|
30 | });
|
31 | it("preserves the 'this' context of the call", function (done) {
|
32 | var foo = { bar: async.cps(function () { return this; }) }, baz = { x: 7 };
|
33 | Promise.promisify(foo.bar.bind(foo))()
|
34 | .then(function (result) { return expect(result).to.equal(foo); })
|
35 | .then(function () { return Promise.promisify(foo.bar).call(baz); })
|
36 | .then(function (result) { return expect(result).to.equal(baz); })
|
37 | .then(function () { return done(); })
|
38 | .catch(done);
|
39 | });
|
40 | it('eventually resolves with its definition\'s returned value', function (done) {
|
41 | var foo = async.cps(function () { return 'blah'; });
|
42 | Promise.promisify(foo)()
|
43 | .then(function (result) { return expect(result).to.equal('blah'); })
|
44 | .then(function () { return done(); })
|
45 | .catch(done);
|
46 | });
|
47 | it('eventually rejects with its definition\'s thrown value', function (done) {
|
48 | var act, exp = new Error('Expected thrown value to match rejection value');
|
49 | var foo = async.cps(function () { throw exp; return 'blah'; });
|
50 | Promise.promisify(foo)()
|
51 | .catch(function (err) { return act = err; })
|
52 | .then(function () {
|
53 | if (!act)
|
54 | done(new Error("Expected function to throw"));
|
55 | else if (act.message !== exp.message)
|
56 | done(exp);
|
57 | else
|
58 | done();
|
59 | });
|
60 | });
|
61 | it('works with await', function (done) {
|
62 | var foo = async.cps(function () { return await(Promise.delay(20).then(function () { return 'blah'; })); });
|
63 | Promise.promisify(foo)()
|
64 | .then(function (result) { return expect(result).to.equal('blah'); })
|
65 | .then(function () { return done(); })
|
66 | .catch(done);
|
67 | });
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | });
|
80 |
|
\ | No newline at end of file |