export function asyncEffect < Args extends unknown [ ] > ( effect ) { let cancelLast = ( ) => { } ; return async ( ... args ) => { cancelLast ( ) ; cancelLast = ( ) => { if ( typeof cleanup === 'function' ) { cleanup ( ) ; } abortController . abort ( ) ; } ; const abortController = new AbortController ( ) ; let cleanup ; try { cleanup = await effect ( abortController . signal , ... args ) ; if ( abortController . signal . aborted ) { if ( typeof cleanup === 'function' ) { cleanup ( ) ; } } } catch ( e ) { if ( e instanceof DOMException ) { if ( e . name === 'AbortError' ) { return ; } } console . error ( e ) ; } } ; }