vitaly-t / iter-ops

Basic operations on iterables
https://vitaly-t.github.io/iter-ops
MIT License
136 stars 5 forks source link

Add async callback support to operator "reduce" #181

Closed vitaly-t closed 1 year ago

vitaly-t commented 1 year ago

Most of operators that provide a callback support async result from it, where it may be useful.

There maybe cases when reduce could benefit from handling async callbacks also.

vitaly-t commented 1 year ago

Closing, for now, because it creates questions like - should the initial value support a promise then also?

It does create some ambiguities, hence the reason why it wasn't supported from start.

vitaly-t commented 1 year ago

@RebeccaStevens This one is actually a good exercise for the brain, and a worthy addition :wink:

So I am re-opening it.

Again, I'm just not sure what this bears for the initial value - should it allow passing in a promise also? This needs to be investigated.

vitaly-t commented 1 year ago

b.t.w. what prompted me to get back to this was this recent question.

vitaly-t commented 1 year ago

I think that in terms of functionality, all it takes is to replace this line in async version:

return this.next();

with this one:

return isPromiseLike(value) ? value.then(() => this.next()) : this.next();

And that's it.

However, in terms of function signature types update, that's where I'm lost. I tried a few things, but they didn't look right. so I figured I'd leave the joy of types to yourself, as you are the expert in types resolution, to figure out how to update the signature properly...

@RebeccaStevens

vitaly-t commented 1 year ago

Added a little refactoring.