@@ -119,4 +119,38 @@ describe('useAsyncState', () => {
119119 expect ( state . value ) . toBe ( 100 )
120120 expect ( initialState ) . toBe ( state )
121121 } )
122+
123+ it ( 'does not set `state` from an outdated execution' , async ( ) => {
124+ const { execute, state } = useAsyncState ( ( returnValue : string , timeout : number ) => promiseTimeout ( timeout ) . then ( ( ) => returnValue ) , '' )
125+ await Promise . all ( [
126+ execute ( 0 , 'foo' , 100 ) ,
127+ execute ( 0 , 'bar' , 50 ) ,
128+ ] )
129+ expect ( state . value ) . toBe ( 'bar' )
130+ } )
131+
132+ it ( 'does not set `isReady` from an outdated execution' , async ( ) => {
133+ const { execute, isReady } = useAsyncState ( promiseTimeout , shallowRef < void > ( ) )
134+ void execute ( 0 , 0 )
135+ void execute ( 0 , 100 )
136+ await promiseTimeout ( 50 )
137+ expect ( isReady . value ) . toBe ( false )
138+ } )
139+
140+ it ( 'does not set `isLoading` from an outdated execution' , async ( ) => {
141+ const { execute, isLoading } = useAsyncState ( promiseTimeout , shallowRef < void > ( ) )
142+ void execute ( 0 , 0 )
143+ void execute ( 0 , 100 )
144+ await promiseTimeout ( 50 )
145+ expect ( isLoading . value ) . toBe ( true )
146+ } )
147+
148+ it ( 'does not set `error` from an outdated execution' , async ( ) => {
149+ const { execute, error } = useAsyncState ( promiseTimeout , shallowRef < void > ( ) )
150+ await Promise . all ( [
151+ execute ( 0 , 100 , true ) ,
152+ execute ( 0 , 0 ) ,
153+ ] )
154+ expect ( error . value ) . toBeUndefined ( )
155+ } )
122156} )
0 commit comments