import { toArray } from './toarray' ; import { ReduceOptions } from './reduceoptions' ; import { throwIfAborted } from '../aborterror' ; template T template R param { AsyncIterable < T > } source param { ReduceOptions < T , R > } options returns { Promise < R > } export async function reduceRight < T , R = T > ( source , options ) { const { [ 'seed' ] : seed , [ 'signal' ] : signal , [ 'callback' ] : callback } = options ; const hasSeed = options . hasOwnProperty ( 'seed' ) ; throwIfAborted ( signal ) ; const array = await toArray ( source , signal ) ; let hasValue = false ; let acc = seed as T | R ; for ( let offset = array . length - 1 ; offset >= 0 ; offset -- ) { const item = array [ offset ] ; if ( hasValue || ( hasValue = hasSeed ) ) { acc = await callback ( < R > acc , item , offset , signal ) ; } else { acc = item ; hasValue = true ; } } if ( ! ( hasSeed || hasValue ) ) { throw new Error ( 'Sequence contains no elements' ) ; } return acc as R ; }