-
I'm trying to refactor my functions that return For example, I have such function: public async resolveSmaIds(
explorationRequest: ExplorationRequest
): Promise<Either<ExternalExplorationError.ExternalExplorationProviderUnavailable, SmaId[]>> {
// ...
// some data preparations with await statement
// ...
return this.externalExplorationProvider.exploreDiscovery(
explorationRequest.requestData as DiscoveryRequestDto,
explorationRequest.hash
); // async call to external service that also returns Promise<Either<ExternalExplorationError.ExternalExplorationProviderUnavailable, SmaId[]>>
} Then i'm trying to refactor it for returning public async resolveSmaIds(
explorationRequest: ExplorationRequest
): TaskEither<ExternalExplorationError.ExternalExplorationProviderUnavailable, SmaId[]>
{
// same body
} Regardless of the return value, I'm getting TS compiler error:
If I drop the
Is this mean that I need to get rid of all |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
you shouldn't need to use import { pipe } from 'fp-ts/function'
import * as TE from 'fp-ts/TaskEither'
// -------------------------------------------------------------------------------------
// promise-based
// -------------------------------------------------------------------------------------
const promiseBasedAPI = async (s: string): Promise<number> => Promise.resolve(s.length)
const promiseBasedProgram = async (s: string): Promise<boolean> => {
const n = await promiseBasedAPI(s)
return n > 0
}
// -------------------------------------------------------------------------------------
// task-based
// -------------------------------------------------------------------------------------
const taskBasedAPI = TE.tryCatchK(promiseBasedAPI, (reason) => new Error(`${reason}`))
const taskBasedProgram = (s: string) =>
pipe(
taskBasedAPI(s),
TE.map((n) => n > 0)
) |
Beta Was this translation helpful? Give feedback.
you shouldn't need to use
await
in most cases, you can usemap
andchain
instead: