import { ErrorString , InvalidOperationException } from "../../shared" ; import { IParallelEnumerable } from "../../types" ; import { ParallelGeneratorType } from "../../types/ParallelGeneratorType" ; param source param predicate throws returns export const lastAsync = async < TSource > ( source , predicate ) : Promise < TSource > => { const dataFunc = source . dataFunc ; switch ( dataFunc . type ) { case ParallelGeneratorType . PromiseToArray : { const values = await dataFunc . generator ( ) ; for ( let i = values . length - 1 ; i >= 0 ; i -- ) { const value = values [ i ] ; if ( await predicate ( value ) === true ) { return value ; } } break ; } case ParallelGeneratorType . ArrayOfPromises : { const promises = dataFunc . generator ( ) ; for ( let i = promises . length - 1 ; i >= 0 ; i -- ) { const value = await promises [ i ] ; if ( await predicate ( value ) === true ) { return value ; } } break ; } case ParallelGeneratorType . PromiseOfPromises : { const promises = await dataFunc . generator ( ) ; for ( let i = promises . length - 1 ; i >= 0 ; i -- ) { const value = await promises [ i ] ; if ( await predicate ( value ) === true ) { return value ; } } break ; } } throw new InvalidOperationException ( ErrorString . NoMatch ) ; } ;