@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
27 lines (25 loc) • 1.02 kB
JavaScript
import { identityAsync } from '../util/identity.js';
import { wrapWithAbort } from './operators/withabort.js';
import { throwIfAborted } from '../aborterror.js';
/**
* Computes the average of the async-iterable sequence.
*
* @param {AsyncIterable<any>} source source async-iterable sequence to compute the average.
* @param {AverageOptions<any>} [options] The options for calculating the average.
* @returns {Promise<number>} A Promise which returns the computed average for the async-iterable sequence.
*/
export async function average(source, options) {
const { ['selector']: selector = identityAsync, ['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;
}
//# sourceMappingURL=average.js.map