@@ -494,8 +494,10 @@ function useFormState<S, P>(
494494 const hook = nextHook ( ) ; // FormState
495495 nextHook ( ) ; // ActionQueue
496496 const stackError = new Error ( ) ;
497- let state ;
497+ let value ;
498498 let debugInfo = null ;
499+ let error = null ;
500+
499501 if ( hook !== null ) {
500502 const actionResult = hook . memoizedState ;
501503 if (
@@ -507,39 +509,45 @@ function useFormState<S, P>(
507509 const thenable : Thenable < Awaited < S >> = ( actionResult : any ) ;
508510 switch ( thenable . status ) {
509511 case 'fulfilled ': {
510- state = thenable . value ;
512+ value = thenable . value ;
511513 debugInfo =
512514 thenable . _debugInfo === undefined ? null : thenable . _debugInfo ;
513515 break ;
514516 }
515517 case 'rejected ': {
516518 const rejectedError = thenable . reason ;
517- throw rejectedError ;
519+ error = rejectedError ;
520+ break ;
518521 }
519522 default:
520523 // If this was an uncached Promise we have to abandon this attempt
521524 // but we can still emit anything up until this point.
522- hookLog.push({
523- primitive : 'FormState' ,
524- stackError : stackError ,
525- value : thenable ,
526- debugInfo :
527- thenable . _debugInfo === undefined ? null : thenable . _debugInfo ,
528- } );
529- throw SuspenseException;
525+ error = SuspenseException;
526+ debugInfo =
527+ thenable._debugInfo === undefined ? null : thenable._debugInfo;
528+ value = thenable;
530529 }
531530 } else {
532- state = ( actionResult : any ) ;
531+ value = ( actionResult : any ) ;
533532 }
534533 } else {
535- state = initialState ;
534+ value = initialState ;
536535 }
536+
537537 hookLog.push({
538538 primitive : 'FormState' ,
539539 stackError : stackError ,
540- value : state ,
540+ value : value ,
541541 debugInfo : debugInfo ,
542542 } );
543+
544+ if (error !== null) {
545+ throw error ;
546+ }
547+
548+ // value being a Thenable is equivalent to error being not null
549+ // i.e. we only reach this point with Awaited< S >
550+ const state = ((value: any): Awaited< S > );
543551 return [state, (payload: P) => { } ];
544552}
545553
0 commit comments