-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack-promises.js
More file actions
163 lines (137 loc) · 3.31 KB
/
Copy pathstack-promises.js
File metadata and controls
163 lines (137 loc) · 3.31 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
var Runtime = require('../');
var Promise = require('es6-promise').Promise;
it('uses the callback when a promise throws', function (done) {
var error = Error('on no!');
var runtime = Runtime.create();
function one () {
return new Promise(function () {
throw error;
});
}
runtime.stack(one)(function (err) {
err.should.be.eql(error);
done();
});
});
it('uses the callback when promises rejects', function (done) {
var error = Error('on no!');
var runtime = Runtime.create();
function one () {
return new Promise(function (resolve, reject) {
reject(error);
});
}
runtime.stack(one)(function (err) {
err.should.be.eql(error);
done();
});
});
it('passes error to onHandleError if no callback was given', function (done) {
var error = new Error('not again...');
var runtime = Runtime.createClass({
onHandleError: function (err) {
if (!(err instanceof Error)) {
return done(new Error('was\'t an instance of error'));
}
err.should.be.eql(error);
done();
}
}).create();
function one () {
return new Promise(function (resolve, reject) {
reject(error);
});
}
runtime.stack(one)();
});
it('runs the callback after completion of all promises', function (done) {
var runtime = Runtime.create();
var count = 0;
function one () {
return new Promise(function (resolve) {
++count; resolve();
});
}
function two () {
return new Promise(function (resolve) {
++count; resolve();
});
}
runtime.stack(one, two)(function (err) {
if (err) { return done(err); }
count.should.be.eql(2);
done();
});
});
it('runs in parallel by default', function (done) {
var runtime = Runtime.create();
var stack = '';
function one () {
return new Promise(function (res) {
setTimeout(function () {
stack += 'one';
res();
}, Math.random()*10);
});
}
function two () {
return new Promise(function (res) {
stack += 'two';
res();
});
}
runtime.stack(one, two)(function (err) {
if (err) { return done(err); }
stack.should.be.eql('twoone');
done();
});
});
it('runs in series with {wait: true}', function (done) {
var runtime = Runtime.create();
var stack = '';
function one () {
return new Promise(function (res) {
setTimeout(function () {
stack += 'one';
res();
}, Math.random()*10);
});
}
function two () {
return new Promise(function (res) {
stack += 'two';
res();
});
}
runtime.stack(one, two, {wait: true})(function (err) {
if (err) { return done(err); }
stack.should.be.eql('onetwo');
done();
});
});
it('passes arguments when it waits', function (done) {
var runtime = Runtime.create();
function one (next, value) {
next.wait = true;
value.should.be.eql(1);
return Promise.resolve(2);
}
function two (next, value) {
value.should.be.eql(2);
next();
}
runtime.stack(one, two)(1, done);
});
it('does NOT pass arguments when fns does NOT wait', function (done) {
var runtime = Runtime.create();
function one (next, value) {
value.should.be.eql(1);
return Promise.resolve(2);
}
function two (next, value) {
value.should.be.eql(1);
next();
}
runtime.stack(one, two)(1, done);
});