1
1
const catchify = require ( './index' ) ;
2
2
const test = require ( 'ava' ) ;
3
3
4
- /// ///////
5
- // catchify
6
4
test ( 'catchify - promise with success' , async t => {
7
5
const [ error , value ] = await catchify ( Promise . resolve ( 1 ) ) ;
8
6
t . is ( error , null ) ;
9
7
t . is ( value , 1 ) ;
10
8
} ) ;
11
9
12
- test ( 'catchify - promise with error' , async t => {
13
- const [ error , value ] = await catchify ( Promise . reject ( new Error ( '3' ) ) ) ;
14
- t . is ( error . message , '3' ) ;
15
- t . is ( value , null ) ;
16
- } ) ;
17
-
18
- test ( 'catchify - value' , async t => {
19
- const [ error , value ] = await catchify ( 1 ) ;
20
- t . is ( error , null ) ;
21
- t . is ( value , 1 ) ;
22
- } ) ;
23
-
24
- /// ///////////////
25
- // catchify.resolve
26
10
test ( 'catchify.resolve - promise with success' , async t => {
27
11
const [ error , value ] = await catchify . resolve ( Promise . resolve ( 1 ) ) ;
28
12
t . is ( error , null ) ;
29
13
t . is ( value , 1 ) ;
30
14
} ) ;
31
15
32
- test ( 'catchify.resolve - promise with error ' , async t => {
33
- const [ error , value ] = await catchify . resolve ( Promise . reject ( new Error ( '1' ) ) ) ;
34
- t . is ( error . message , '1' ) ;
16
+ test ( 'catchify.reject - value ' , async t => {
17
+ const [ error , value ] = await catchify . reject ( 1 ) ;
18
+ t . is ( error , 1 ) ;
35
19
t . is ( value , null ) ;
36
20
} ) ;
37
21
38
- test ( 'catchify.resolve - value' , async t => {
39
- const [ error , value ] = await catchify . resolve ( 1 ) ;
40
- t . is ( error , null ) ;
41
- t . is ( value , 1 ) ;
42
- } ) ;
43
-
44
- /// ////////////
45
- // catchify.race
46
22
test ( 'catchify.race - two values' , async t => {
47
23
const [ error , value ] = await catchify . race ( [ 1 , 2 ] ) ;
48
24
t . is ( error , null ) ;
49
25
t . is ( value , 1 ) ;
50
26
} ) ;
51
27
52
- test ( 'catchify.race - two resolved promises' , async t => {
53
- const [ error , value ] = await catchify . race ( [ Promise . resolve ( 1 ) , Promise . resolve ( 2 ) ] ) ;
54
- t . is ( error , null ) ;
55
- t . is ( value , 1 ) ;
56
- } ) ;
57
-
58
- test ( 'catchify.race - two promises, one rejected and one resolved' , async t => {
59
- const [ error , value ] = await catchify . race ( [
60
- Promise . reject ( new Error ( '1' ) ) ,
61
- Promise . resolve ( 2 )
62
- ] ) ;
63
- t . is ( error . message , '1' ) ;
64
- t . is ( value , null ) ;
65
- } ) ;
66
-
67
- test ( 'catchify.race - two promises, both resolve, fastest wins' , async t => {
68
- const [ error , value ] = await catchify . race ( [
69
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 1 ) , 10 ) ) ,
70
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 2 ) , 100 ) )
71
- ] ) ;
72
- t . is ( error , null ) ;
73
- t . is ( value , 1 ) ;
74
- } ) ;
75
-
76
- test ( 'catchify.race - two promises, reject finishes first' , async t => {
77
- const [ error , value ] = await catchify . race ( [
78
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => reject ( new Error ( '1' ) ) , 10 ) ) ,
79
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 2 ) , 100 ) )
80
- ] ) ;
81
- t . deepEqual ( error , new Error ( '1' ) ) ;
82
- t . is ( value , null ) ;
83
- } ) ;
84
-
85
- test ( 'catchify.race - two promises, resolve finishes first' , async t => {
86
- const [ error , value ] = await catchify . race ( [
87
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => reject ( new Error ( '1' ) ) , 100 ) ) ,
88
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 2 ) , 10 ) )
89
- ] ) ;
90
- t . is ( error , null ) ;
91
- t . is ( value , 2 ) ;
92
- } ) ;
93
-
94
- /// ///////////
95
- // catchify.all
96
28
test ( 'catchify.all - two values resolved' , async t => {
97
29
const [ error , [ value1 , value2 ] ] = await catchify . all ( [ 1 , 2 ] ) ;
98
30
t . is ( error , null ) ;
99
31
t . is ( value1 , 1 ) ;
100
32
t . is ( value2 , 2 ) ;
101
33
} ) ;
102
34
103
- test ( 'catchify.all - one of three promises rejected' , async t => {
104
- const [ error , [ value1 , value2 , value3 ] ] = await catchify . all ( [
105
- Promise . resolve ( 1 ) ,
106
- Promise . resolve ( 2 ) ,
107
- Promise . reject ( new Error ( '3' ) )
108
- ] ) ;
109
- t . is ( error . message , '3' ) ;
110
- t . is ( value1 , undefined ) ;
111
- t . is ( value2 , undefined ) ;
112
- t . is ( value3 , undefined ) ;
113
- } ) ;
114
-
115
- test ( 'catchify.all - object with values' , async t => {
116
- const [ error , { a, b} ] = await catchify . all ( { a : 1 , b : 2 } ) ;
117
- t . is ( error , null ) ;
118
- t . is ( a , 1 ) ;
119
- t . is ( b , 2 ) ;
120
- } ) ;
121
-
122
- test ( 'catchify.all - object with promises that both resolve' , async t => {
123
- const [ error , { a, b} ] = await catchify . all ( {
124
- a : Promise . resolve ( 1 ) ,
125
- b : Promise . resolve ( 2 )
126
- } ) ;
127
- t . is ( error , null ) ;
128
- t . is ( a , 1 ) ;
129
- t . is ( b , 2 ) ;
130
- } ) ;
131
-
132
- test ( 'catchify.all - object with promises and one rejects' , async t => {
133
- const [ error , values ] = await catchify . all ( {
134
- a : Promise . resolve ( 1 ) ,
135
- b : Promise . reject ( new Error ( '2' ) )
136
- } ) ;
137
- t . is ( error . message , '2' ) ;
138
- t . deepEqual ( values , { } ) ;
139
- } ) ;
140
-
141
- /// //////////////
142
- // catchify.reject
143
- test ( 'catchify.reject - value' , async t => {
144
- const [ error , value ] = await catchify . reject ( 1 ) ;
145
- t . is ( error , 1 ) ;
146
- t . is ( value , null ) ;
147
- } ) ;
148
-
149
- /// ////////////
150
- // catchify.some
151
35
test ( 'catchify.some - resolve and reject' , async t => {
152
36
const [ [ error1 , error2 ] , [ value1 , value2 ] ] = await catchify . some ( [
153
37
Promise . resolve ( 1 ) ,
@@ -160,51 +44,6 @@ test('catchify.some - resolve and reject', async t => {
160
44
t . is ( value2 , null ) ;
161
45
} ) ;
162
46
163
- test ( 'catchify.some - resolve and resolve' , async t => {
164
- const [ errors , values ] = await catchify . some ( [
165
- Promise . resolve ( 1 ) ,
166
- Promise . resolve ( 2 )
167
- ] ) ;
168
-
169
- t . deepEqual ( errors , [ null , null ] ) ;
170
- t . deepEqual ( values , [ 1 , 2 ] ) ;
171
- } ) ;
172
-
173
- test ( 'catchify.some - three promises one rejects' , async t => {
174
- const [ errors , values ] = await catchify . some ( [
175
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 1 ) , 10 ) ) ,
176
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 2 ) , 15 ) ) ,
177
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => reject ( new Error ( '3' ) ) , 5 ) )
178
- ] ) ;
179
-
180
- t . deepEqual ( errors , [ null , null , new Error ( '3' ) ] ) ;
181
- t . deepEqual ( values , [ 1 , 2 , null ] ) ;
182
- } ) ;
183
-
184
- test ( 'catchify.some - object with three resolved values' , async t => {
185
- const [ errors , values ] = await catchify . some ( {
186
- a : Promise . resolve ( 1 ) ,
187
- b : Promise . resolve ( 2 ) ,
188
- c : Promise . resolve ( 3 )
189
- } ) ;
190
-
191
- t . deepEqual ( errors , { a : null , b : null , c : null } ) ;
192
- t . deepEqual ( values , { a : 1 , b : 2 , c : 3 } ) ;
193
- } ) ;
194
-
195
- test ( 'catchify.some - object with two resolved and one rejected' , async t => {
196
- const [ errors , values ] = await catchify . some ( {
197
- a : Promise . resolve ( 1 ) ,
198
- b : Promise . reject ( new Error ( '2' ) ) ,
199
- c : Promise . resolve ( 3 )
200
- } ) ;
201
-
202
- t . deepEqual ( errors , { a : null , b : new Error ( '2' ) , c : null } ) ;
203
- t . deepEqual ( values , { a : 1 , b : null , c : 3 } ) ;
204
- } ) ;
205
-
206
- /// /////////////
207
- // catchify.limit
208
47
test ( 'catchify.limit - three promises, last one rejects' , async t => {
209
48
const [ errors , values ] = await catchify . limit ( [
210
49
new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 1 ) , 5 ) ) ,
@@ -214,73 +53,3 @@ test('catchify.limit - three promises, last one rejects', async t => {
214
53
t . deepEqual ( errors , [ null , null , new Error ( '3' ) ] ) ;
215
54
t . deepEqual ( values , [ 1 , 2 , null ] ) ;
216
55
} ) ;
217
-
218
- test ( 'catchify.limit - three promises, second one rejects' , async t => {
219
- const [ errors , values ] = await catchify . limit ( [
220
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 1 ) , 5 ) ) ,
221
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => reject ( new Error ( '2' ) ) , 5 ) ) ,
222
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 3 ) , 5 ) )
223
- ] ) ;
224
- t . deepEqual ( errors , [ null , new Error ( '2' ) , null ] ) ;
225
- t . deepEqual ( values , [ 1 , null , 3 ] ) ;
226
- } ) ;
227
-
228
- test ( 'catchify.limit - three promises, first one rejects' , async t => {
229
- const [ errors , values ] = await catchify . limit ( [
230
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 1 ) , 5 ) ) ,
231
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => reject ( new Error ( '2' ) ) , 5 ) ) ,
232
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 3 ) , 5 ) )
233
- ] ) ;
234
- t . deepEqual ( errors , [ null , new Error ( '2' ) , null ] ) ;
235
- t . deepEqual ( values , [ 1 , null , 3 ] ) ;
236
- } ) ;
237
-
238
- test ( 'catchify.limit - three promises, first one rejects' , async t => {
239
- const [ errors , values ] = await catchify . limit ( [
240
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => reject ( new Error ( '1' ) ) , 5 ) ) ,
241
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 2 ) , 5 ) ) ,
242
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 3 ) , 5 ) )
243
- ] ) ;
244
- t . deepEqual ( errors , [ new Error ( '1' ) , null , null ] ) ;
245
- t . deepEqual ( values , [ null , 2 , 3 ] ) ;
246
- } ) ;
247
-
248
- test ( 'catchify.limit - three promises, first one rejects, limit=1, exitOnError=true' , async t => {
249
- const [ errors , values ] = await catchify . limit ( [
250
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => reject ( new Error ( '1' ) ) , 5 ) ) ,
251
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 2 ) , 5 ) ) ,
252
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 3 ) , 5 ) )
253
- ] , 1 , true ) ;
254
- t . deepEqual ( errors , [ new Error ( '1' ) ] ) ;
255
- t . deepEqual ( values , [ null ] ) ;
256
- } ) ;
257
-
258
- test ( 'catchify.limit - three promises, second one rejects, limit=1, exitOnError=true' , async t => {
259
- const [ errors , values ] = await catchify . limit ( [
260
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 1 ) , 5 ) ) ,
261
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => reject ( new Error ( '2' ) ) , 5 ) ) ,
262
- new Promise ( ( resolve , reject ) => setTimeout ( ( ) => resolve ( 3 ) , 5 ) )
263
- ] , 1 , true ) ;
264
- t . deepEqual ( errors , [ null , new Error ( '2' ) ] ) ;
265
- t . deepEqual ( values , [ 1 , null ] ) ;
266
- } ) ;
267
-
268
- test ( 'catchify.limit - object with three resolved values' , async t => {
269
- const [ errors , values ] = await catchify . limit ( {
270
- a : Promise . resolve ( 1 ) ,
271
- b : Promise . resolve ( 2 ) ,
272
- c : Promise . resolve ( 3 )
273
- } ) ;
274
- t . deepEqual ( errors , { a : null , b : null , c : null } ) ;
275
- t . deepEqual ( values , { a : 1 , b : 2 , c : 3 } ) ;
276
- } ) ;
277
-
278
- test ( 'catchify.limit - object with two resolved values and one rejected value' , async t => {
279
- const [ errors , values ] = await catchify . limit ( {
280
- a : Promise . resolve ( 1 ) ,
281
- b : Promise . reject ( new Error ( '2' ) ) ,
282
- c : Promise . resolve ( 3 )
283
- } ) ;
284
- t . deepEqual ( errors , { a : null , b : new Error ( '2' ) , c : null } ) ;
285
- t . deepEqual ( values , { a : 1 , b : null , c : 3 } ) ;
286
- } ) ;
0 commit comments