chain overloading
See original GitHub issueHey supermacro, thanks so much for this library 😃 I’m starting to use it in one of my projects and love it so far! Anyway I was just wondering if you considered using function overloading for the chain
API? I’m imaging something similar to the pipe
API in fp-ts
.
Here is kinda what I’m thinking although this has not been tested:
export function chain<T1, T2, E>(r1: Promise<Result<T1, E>>, r2: (v: T1) => Promise<Result<T2, E>>): Promise<Result<T2, E>>;
export function chain<T1, T2, T3, E>(r1: Promise<Result<T1, E>>, r2: (v: T1) => Ok<T2, E> | Err<T2, E> | Promise<Result<T2, E>>, r3: (v: T2) => Promise<Result<T3, E>>): Promise<Result<T3, E>>;
export function chain<T1, T2, T3, T4, E>(r1: Promise<Result<T1, E>>, r2: (v: T1) => Ok<T2, E> | Err<T2, E> | Promise<Result<T2, E>>, r3: (v: T2) => Ok<T3, E> | Err<T3, E> | Promise<Result<T3, E>>, r4: (v: T3) => Promise<Result<T4, E>>): Promise<Result<T4, E>>;
export function chain<T1, T2, T3, T4, T5, E>(r1: Promise<Result<T1, E>>, r2: (v: T1) => Ok<T2, E> | Err<T2, E> | Promise<Result<T2, E>>, r3: (v: T2) => Ok<T3, E> | Err<T3, E> | Promise<Result<T3, E>>, r4: (v: T3) => Ok<T4, E> | Err<T4, E> | Promise<Result<T4, E>>, r5: (v: T4) => Promise<Result<T5, E>>): Promise<Result<T5, E>>;
export function chain<T1, T2, T3, T4, T5, T6, E>(r1: Promise<Result<T1, E>>, r2: (v: T1) => Ok<T2, E> | Err<T2, E> | Promise<Result<T2, E>>, r3: (v: T2) => Ok<T3, E> | Err<T3, E> | Promise<Result<T3, E>>, r4: (v: T3) => Ok<T4, E> | Err<T4, E> | Promise<Result<T4, E>>, r5: (v: T4) => Ok<T5, E> | Err<T5, E> | Promise<Result<T5, E>>, r6: (v: T5) => Promise<Result<T6, E>>): Promise<Result<T6, E>>;
export function chain<T1, T2, T3, T4, T5, T6, T7, E>(r1: Promise<Result<T1, E>>, r2: (v: T1) => Ok<T2, E> | Err<T2, E> | Promise<Result<T2, E>>, r3: (v: T2) => Ok<T3, E> | Err<T3, E> | Promise<Result<T3, E>>, r4: (v: T3) => Ok<T4, E> | Err<T4, E> | Promise<Result<T4, E>>, r5: (v: T4) => Ok<T5, E> | Err<T5, E> | Promise<Result<T5, E>>, r6: (v: T5) => Ok<T6, E> | Err<T6, E> | Promise<Result<T6, E>>, r7: (v: T6) => Promise<Result<T7, E>>): Promise<Result<T7, E>>;
export function chain<T1, T2, T3, T4, T5, T6, T7, T8, E>(r1: Promise<Result<T1, E>>, r2: (v: T1) => Ok<T2, E> | Err<T2, E> | Promise<Result<T2, E>>, r3: (v: T2) => Ok<T3, E> | Err<T3, E> | Promise<Result<T3, E>>, r4: (v: T3) => Ok<T4, E> | Err<T4, E> | Promise<Result<T4, E>>, r5: (v: T4) => Ok<T5, E> | Err<T5, E> | Promise<Result<T5, E>>, r6: (v: T5) => Ok<T6, E> | Err<T6, E> | Promise<Result<T6, E>>, r7: (v: T6) => Ok<T7, E> | Err<T7, E> | Promise<Result<T7, E>>, r8: (v: T7) => Promise<Result<T8, E>>): Promise<Result<T8, E>>;
export async function chain(
start: Promise<Result<any, any>>,
...transforms: any
): Promise<Result<any, any>> {
return await (transforms as Array<(v: any) => Promise<Result<any, any>>>).reduce(
async (current, f) => {
const mapped = await (await current).asyncMap(f);
return mapped.andThen((inner) => inner);
},
start,
);
}
Let me know your thoughts!
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
design patterns - How to correctly chain overloaded functions?
I prefer the first method. The possibility of duplicated default values all over the place can severely reduce maintainability.
Read more >How do I use a chain of operator overloads without modifying ...
So the question is: is there any way I could use a chain of operator overloads that do not modify the operands without...
Read more >Roller Chain Failure: Causes and Strategies for Prevention
1 Roller Chain Overloading. One of the principal causes of roller chain failure is bearing consistently high loads. The roller chain is ...
Read more >How is proper chain tension measured and why is it important?
Proper chain tension is critical to achieving acceptable service life, as excessive tension can cause accelerated wear or chain overload and excessive slack ......
Read more >PB-overloading chains. Considering these existing problems ...
But, in theory, as long as the time between the first task and the last task in the PB-overloading chain is less than...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jsmith @paduc I’m currently at work so I can’t really spend the time thinking about this, but here’s a new github issue: https://github.com/supermacro/neverthrow/issues/39
Let’s move the convo there.
Yes, that will be perfect! I’m using
neverthrow
to define a set of express routes which use a combination of synchronous and asynchronous operations so this new API should be a good replacement.I’m building a remote git api and currently have something like this:
But will just be able to use
ResultAsync
andmap
!Anyway, thanks for your help 😃