| 584 | async reduce<T>(iteratee: DatasetReducer<T, Data>, memo: T, options?: DatasetIteratorOptions): Promise<T>; |
| 585 | |
| 586 | async reduce<T = Data>( |
| 587 | iteratee: DatasetReducer<T, Data>, |
| 588 | memo?: T, |
| 589 | options: DatasetIteratorOptions = {}, |
| 590 | ): Promise<T | undefined> { |
| 591 | checkStorageAccess(); |
| 592 | |
| 593 | let currentMemo: T | undefined = memo; |
| 594 | |
| 595 | const wrappedFunc: DatasetConsumer<Data> = async (item, index) => { |
| 596 | if (index === 0 && currentMemo === undefined) { |
| 597 | currentMemo = item; |
| 598 | } else { |
| 599 | // We are guaranteed that currentMemo is instanciated, since we are either not on |
| 600 | // the first iteration, or memo was already set by the user. |
| 601 | currentMemo = await iteratee(currentMemo as T, item, index); |
| 602 | } |
| 603 | }; |
| 604 | |
| 605 | await this.forEach(wrappedFunc, options); |
| 606 | return currentMemo; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * Iterates over dataset items using an async generator, |