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 |
|
7 |
|
8 | describe('The await(...) function', function () {
|
9 | it('throws if not called within a suspendable function', function () {
|
10 | expect(function () { return await(111); }).to.throw(Error);
|
11 | });
|
12 | it('suspends the suspendable function until the expression produces a result', function (done) {
|
13 | var x = 5;
|
14 | var foo = async(function () {
|
15 | await(Promise.delay(40));
|
16 | x = 7;
|
17 | await(Promise.delay(40));
|
18 | x = 9;
|
19 | });
|
20 | foo();
|
21 | Promise.delay(20)
|
22 | .then(function () { return expect(x).to.equal(5); })
|
23 | .then(function () { return Promise.delay(40); })
|
24 | .then(function () { return expect(x).to.equal(7); })
|
25 | .then(function () { return Promise.delay(40); })
|
26 | .then(function () { return expect(x).to.equal(9); })
|
27 | .then(function () { return done(); })
|
28 | .catch(done);
|
29 | expect(x).to.equal(5);
|
30 | });
|
31 | it('resumes the suspendable function with the value of the awaited expression', function (done) {
|
32 | var foo = async(function () { return await(Promise.delay(20).then(function () { return 'blah'; })); });
|
33 | foo()
|
34 | .then(function (result) { return expect(result).to.equal('blah'); })
|
35 | .then(function () { return done(); })
|
36 | .catch(done);
|
37 | });
|
38 | it('throws into the suspendable function the error produced by the awaited expression', function (done) {
|
39 | var foo = async(function () { return await(Promise.delay(20).then(function () { throw new Error('blah'); })); });
|
40 | foo()
|
41 | .then(function () { return done(new Error('foo() should have rejected')); })
|
42 | .catch(function (err) { expect(err.message).to.equal('blah'); done(); });
|
43 | });
|
44 | it('resumes the suspendable function with all the results of a concurrent expression', function (done) {
|
45 | var foo = async(function () { return await(Promise.delay(40).then(function () { return 'foo'; })); });
|
46 | var bar = async(function () { return await(Promise.delay(20).then(function () { return 'bar'; })); });
|
47 | var all = async(function () { return await([foo(), bar()]); });
|
48 | all()
|
49 | .then(function (result) { return expect(result).to.deep.equal(['foo', 'bar']); })
|
50 | .then(function () { return done(); })
|
51 | .catch(done);
|
52 | });
|
53 | it('throws into the suspendable function the first error in a concurrent expression', function (done) {
|
54 | var foo = async(function () { return await(Promise.delay(40).then(function () { throw new Error('foo'); })); });
|
55 | var bar = async(function () { return await(Promise.delay(20).then(function () { throw new Error('bar'); })); });
|
56 | var all = async(function () { return await([foo(), bar()]); });
|
57 | all()
|
58 | .then(function () { return done(new Error('all() should have rejected')); })
|
59 | .catch(function (err) { expect(err.message).to.equal('bar'); done(); });
|
60 | });
|
61 | });
|
62 |
|
63 |
|
\ | No newline at end of file |