File tree 2 files changed +31
-11
lines changed 2 files changed +31
-11
lines changed Original file line number Diff line number Diff line change
1
+ var PromiseRouter = require ( "../src/PromiseRouter" ) . default ;
2
+
3
+ describe ( "PromiseRouter" , ( ) => {
4
+
5
+ it ( "should properly handle rejects" , ( done ) => {
6
+ var router = new PromiseRouter ( ) ;
7
+ router . route ( "GET" , "/dummy" , ( req ) => {
8
+ return Promise . reject ( {
9
+ error : "an error" ,
10
+ code : - 1
11
+ } )
12
+ } , ( req ) => {
13
+ fail ( "this should not be called" ) ;
14
+ } ) ;
15
+
16
+ router . routes [ 0 ] . handler ( { } ) . then ( ( result ) => {
17
+ console . error ( result ) ;
18
+ fail ( "this should not be called" ) ;
19
+ done ( ) ;
20
+ } , ( error ) => {
21
+ expect ( error . error ) . toEqual ( "an error" ) ;
22
+ expect ( error . code ) . toEqual ( - 1 ) ;
23
+ done ( ) ;
24
+ } ) ;
25
+ } ) ;
26
+ } )
Original file line number Diff line number Diff line change @@ -49,17 +49,11 @@ export default class PromiseRouter {
49
49
if ( handlers . length > 1 ) {
50
50
const length = handlers . length ;
51
51
handler = function ( req ) {
52
- var next = function ( i , req , res ) {
53
- if ( i == length ) {
54
- return res ;
55
- }
56
- let result = handlers [ i ] ( req ) ;
57
- if ( ! result || typeof result . then !== "function" ) {
58
- result = Promise . resolve ( result ) ;
59
- }
60
- return result . then ( ( res ) => ( next ( i + 1 , req , res ) ) ) ;
61
- }
62
- return next ( 0 , req ) ;
52
+ return handlers . reduce ( ( promise , handler ) => {
53
+ return promise . then ( ( result ) => {
54
+ return handler ( req ) ;
55
+ } ) ;
56
+ } , Promise . resolve ( ) ) ;
63
57
}
64
58
}
65
59
You can’t perform that action at this time.
0 commit comments