You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When consuming a stream with .reduce, I'd like to be able to do some async operations inside the iterator/reducer function and have the stream wait for their completion before consuming next element.
However the stream is being consumed as fast as possible, without waiting for async operation inside the reducer to complete. The output of running the above is:
You can use wrapAsync which converts a promise returning function (e.g. an async fn) into a stream which emits a single value, e.g.
constwrapAsync=(fn)=>{return(...args)=>{letpromise;try{promise=fn.apply(_,args);if(!_.isObject(promise)||!_.isFunction(promise.then)){return_.fromError(newError('Wrapped function did not return a promise'));}return_(promise);}catch(e){return_.fromError(e);}};};
@richardscarrott thanks for the tip. I am already using a similar approach with flatMap, however I'd like to use the same technique with reduce so that I can pass some state along as I'm processing individual elements. I know I can achieve a similar result by using flatMap with some mutable state in the outer scope but I'd prefer to avoid it.
When consuming a stream with
.reduce
, I'd like to be able to do some async operations inside theiterator/reducer
function and have the stream wait for their completion before consuming next element.Here's what I'm doing right now:
However the stream is being consumed as fast as possible, without waiting for async operation inside the reducer to complete. The output of running the above is:
while I'd like it to be:
The text was updated successfully, but these errors were encountered: