Promised arrays with map/reduce?

42 views
Skip to first unread message

Wout Mertens

unread,
Jul 10, 2013, 8:16:36 AM7/10/13
to q-con...@googlegroups.com
This idea struck me, I'd be grateful for your input:

How about some sort of promised array, that you can run forEach, map and reduce on. These functions are executed while the array is still building and the eventual length isn't known. Getting .length returns a promise for the eventual resolution of the array.

Example use case: getting a large amount of values asynchronously, and processing them while they come in.

I suppose that if you're not interested in the resulting array itself you should use a Q.queue?
Perhaps this array can be simply implemented with Q.queue?

Wout.

Kris Kowal

unread,
Jul 10, 2013, 2:13:34 PM7/10/13
to Q Continuum
Have you seen the patch I landed for promise streams?


I’m adding forEach, map, reduce, and buffer to Q. They all accept an optional "maxInFlight" and "notify(inFlight, maxInFlight)" arguments. So they support back-pressure and throttling.

They are built on top of the new ES6 iterator protocol and promise queues.  The promise queue is a convenient asynchronous transport for iterations, but the input of any of these arguments can be an array or any duck-iterator (implements "next") or duck-iterable (implements "iterator"). The output is consistently a promise queue for iterations.

The trick is that the iterator protocol has good ways to model "next value", "next value for key", "end of stream", and "exception thrown".

They are very experimental and I would love to have feedback.

Kris Kowal

Reply all
Reply to author
Forward
0 new messages