import { AsyncIterableX } from '../asynciterablex' ; import { MonoTypeOperatorAsyncFunction } from '../../interfaces' ; import { wrapWithAbort } from './withabort' ; import { throwIfAborted } from '../../aborterror' ; export class FinallyAsyncIterable < TSource > extends AsyncIterableX < TSource > { private _source : AsyncIterable < TSource > ; private _action : ( ) => any | Promise < any > ; constructor ( source , action ) { super ( ) ; this . _source = source ; this . _action = action ; } async * [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; try { for await ( const item of wrapWithAbort ( this . _source , signal ) ) { yield item ; } } finally { await this . _action ( ) ; } } } template TSource param { ( ( ) => void | Promise < void > ) } action returns { MonoTypeOperatorAsyncFunction < TSource > } export function finalize < TSource > ( action ) { return function finalizeOperatorFunction ( source ) : AsyncIterableX < TSource > { return new FinallyAsyncIterable < TSource > ( source , action ) ; } ; }