import { ErrorString , InvalidOperationException } from "../../shared" ; import { IParallelEnumerable } from "../../types" ; import { toArray } from "./toArray" ; param source param predicate throws returns export const singleAsync = async < TSource > ( source , predicate ) : Promise < TSource > => { const results = await toArray ( source ) ; let hasValue = false ; let singleValue = null ; for ( const value of results ) { if ( await predicate ( value ) === true ) { if ( hasValue === true ) { throw new InvalidOperationException ( ErrorString . MoreThanOneMatchingElement ) ; } else { hasValue = true ; singleValue = value ; } } } if ( hasValue === false ) { throw new InvalidOperationException ( ErrorString . NoMatch ) ; } return singleValue as TSource ; } ;