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
One of our use cases of ResultAsync.combine is to act as a Promise.all for several external API calls, which is great for combining multiple ResultAsyncs running in parallel.
The array of these () => ResultAsync<T,E> can become quite large, so for some scenarios a sequential approach proved to solve some rate-limiting issues.
I would imagine there are other scenarios where sequentially running an array of functions returning ResultAsyncs could be useful, such as:
Having certain dependencies between each task/function, or where the outcome of one affects the other.
A primitive way of Rate Limiting/Throttling
In non idempotent operations, order of each task is important and running them sequentially would be a must. (Specially on longer chains)
Batch processing
It can also be used as a way to prevent error propagation, if none can fail, as soon as one does no additional calls are made.
ha! great minds think alike. I wrote this a few days ago:
/** * Run a list of functions sequentially and return immediately when the * first error is generated. * If no errors are found, return a list of result values in the same order. */exportfunctionrunInSequence<T,E>(fns: ReadonlyArray<()=>ResultAsync<T,E>>,): ResultAsync<Array<T>,E>{construn=async()=>{constokValues: Array<T>=[];for(constfnoffns){constresult=awaitfn();if(result.isErr()){returnerr(result.error);}okValues.push(result.value);}returnok(okValues);};returnnewResultAsync(run());}
One of our use cases of
ResultAsync.combine
is to act as aPromise.all
for several external API calls, which is great for combining multiple ResultAsyncs running in parallel.The array of these
() => ResultAsync<T,E>
can become quite large, so for some scenarios a sequential approach proved to solve some rate-limiting issues.I would imagine there are other scenarios where sequentially running an array of functions returning ResultAsyncs could be useful, such as:
It can also be used as a way to prevent error propagation, if none can fail, as soon as one does no additional calls are made.
The code for this could resemble something like:
@pyrho came up with this.
The text was updated successfully, but these errors were encountered: