UNPKG

883 BJavaScriptView Raw
1import { identityAsync } from '../util/identity';
2import { wrapWithAbort } from './operators/withabort';
3import { throwIfAborted } from '../aborterror';
4/**
5 * Computes the sum of a sequence of values.
6 *
7 * @export
8 * @param {AsyncIterable<any>} source A sequence of values to calculate the sum.
9 * @param {MathOptions<any>} [options] Optional options for providing a selector, thisArg and abort signal.
10 * @returns {Promise<number>} A promise containing the sum of the sequence of values.
11 */
12export async function sum(source, options) {
13 const { ['selector']: selector = identityAsync, ['signal']: signal, ['thisArg']: thisArg, } = options || {};
14 throwIfAborted(signal);
15 let value = 0;
16 for await (const item of wrapWithAbort(source, signal)) {
17 value += await selector.call(thisArg, item, signal);
18 }
19 return value;
20}
21
22//# sourceMappingURL=sum.mjs.map