import { identityAsync } from '../util/identity' ; import { wrapWithAbort } from './operators/withabort' ; import { throwIfAborted } from '../aborterror' ; import { MathOptions } from './mathoptions' ; param { AsyncIterable < number > } source param { AverageOptions < number > } options returns { Promise < number > } export async function average ( source , options ? ) ; template TSource param { AsyncIterable < TSource > } source param { AverageOptions < TSource > } options returns { Promise < number > } export async function average < TSource > ( source , options ? ) ; param { AsyncIterable < any > } source param { AverageOptions < any > } options returns { Promise < number > } export async function average ( source , options ? ) { const { [ 'selector' ] : selector = identityAsync as any , [ 'signal' ] : signal , [ 'thisArg' ] : thisArg , } = options || { } ; throwIfAborted ( signal ) ; let sum = 0 ; let count = 0 ; for await ( const item of wrapWithAbort ( source , signal ) ) { sum += await selector . call ( thisArg , item , signal ) ; count ++ ; } if ( count === 0 ) { throw new Error ( 'Empty collection' ) ; } return sum / count ; }