@@ -159,27 +159,38 @@ export function guardToPromiseFn(
159
159
}
160
160
161
161
// wrapping with Promise.resolve allows it to work with both async and sync guards
162
- let guardCall = Promise . resolve (
163
- guard . call (
164
- record && record . instances [ name ! ] ,
165
- to ,
166
- from ,
167
- __DEV__ ? canOnlyBeCalledOnce ( next , to , from ) : next
168
- )
162
+ const guardReturn = guard . call (
163
+ record && record . instances [ name ! ] ,
164
+ to ,
165
+ from ,
166
+ __DEV__ ? canOnlyBeCalledOnce ( next , to , from ) : next
169
167
)
168
+ let guardCall = Promise . resolve ( guardReturn )
170
169
171
170
if ( guard . length < 3 ) guardCall = guardCall . then ( next )
172
- if ( __DEV__ && guard . length > 2 )
173
- guardCall = guardCall . then ( ( ) => {
171
+ if ( __DEV__ && guard . length > 2 ) {
172
+ const message = `The "next" callback was never called inside of ${
173
+ guard . name ? '"' + guard . name + '"' : ''
174
+ } :\n${ guard . toString ( ) } \n. If you are returning a value instead of calling "next", make sure to remove the "next" parameter from your function.`
175
+ if ( typeof guardReturn === 'object' && 'then' in guardReturn ) {
176
+ guardCall = guardCall . then ( resolvedValue => {
177
+ // @ts -ignore: _called is added at canOnlyBeCalledOnce
178
+ if ( ! next . _called ) {
179
+ warn ( message )
180
+ return Promise . reject ( new Error ( 'Invalid navigation guard' ) )
181
+ }
182
+ return resolvedValue
183
+ } )
184
+ // TODO: test me!
185
+ } else if ( guardReturn !== undefined ) {
174
186
// @ts -ignore: _called is added at canOnlyBeCalledOnce
175
- if ( ! next . _called )
176
- warn (
177
- `The "next" callback was never called inside of ${
178
- guard . name ? '"' + guard . name + '"' : ''
179
- } :\n${ guard . toString ( ) } \n. If you are returning a value instead of calling "next", make sure to remove the "next" parameter from your function.`
180
- )
181
- return Promise . reject ( new Error ( 'Invalid navigation guard' ) )
182
- } )
187
+ if ( ! next . _called ) {
188
+ warn ( message )
189
+ reject ( new Error ( 'Invalid navigation guard' ) )
190
+ return
191
+ }
192
+ }
193
+ }
183
194
guardCall . catch ( err => reject ( err ) )
184
195
} )
185
196
}
0 commit comments