tc39 / proposal-async-iterator-helpers

Methods for working with async iterators in ECMAScript
https://tc39.es/proposal-async-iterator-helpers/
95 stars 3 forks source link

Do methods that return one value return a Promise or an AsyncIterable of one value? #9

Closed benlesh closed 1 year ago

benlesh commented 1 year ago

I couldn't quite make it out from reading the spec HTML in the repo.

For example, asyncIterable.reduce((acc, n) => acc + n, 0) does that return a Promise<number> or an AsyncIterable<number>.

It seems like the Promise<number> would be more useful in a world with async await, however, it would mean that any other APIs that want to follow suit don't have a predictable template to follow... then again, Array#reduce can return anything at all, and Array#find (et al) don't return an array, they return the value.

bakkot commented 1 year ago

asyncIterable.reduce

To be clear, these methods are on iterators, not on iterables. There is no generic "async iterable" class where we could put stuff.


To answer the question: Promise<number>.

This is similar to the sync versions, where e.g. syncIterator.reduce((acc, n) => acc + n, 0) will return a number, rather than an iterator of one value.

(Closing as answered but feel free to re-open if there's more.)