In fp-ts, I'm trying to chain some potentially failing async tasks together with TaskEither but I need to use the results from intermediate tasks later on down the chain.
In this example:
const getFoo = (a: string): Promise<Foo> => {};
const getBar = (foo: Foo): Promise<Bar> => {};
const mkFooBar = (foo: Foo, bar: Bar): Promise<FooBar> => {};
const async main1: Promise<FooBar> => {
const a = "a";
const foo = await getFoo(a);
const bar = await getBar(foo);
const fooBar = await mkFooBar(foo, bar);
return Promise.resolve(fooBar);
};
const main2: Promise<FooBar> => {
const a = "a";
return pipe(
TE.tryCatch(() => getFoo(a), e => e),
TE.chain(foo => TE.tryCatch(() => getBar(foo), e => e)),
TE.chain(bar => TE.tryCatch(() => mkFooBar(??, bar), e => e))
);
};
the main1 function is an async/await-style solution to this problem. What I'm trying to do is emulate something like this in a fp-ts chain-style. main2 is my attempt at this.
Because the async/await version introduces all the intermediate results into the local scope (i.e. foo and bar), it's easy to call mkFooBar which depends on both those results.
But in the fp-ts version, the intermediate results are trapped in the scope of each task.
The only way I can think to make this version work would be to make either the async functions themselves (i.e. getFoo and getBar) also return their arguments, or perhaps the TaskEither wrappers return the arguments so that they can then be passed on to the next function in the chain.
Would this be the correct way to do this? Or is there a simpler version which more closely resembles the async/await version?