Issue
This Content is from Stack Overflow. Question asked by Ricardo Silva
I have the following code:
const tError = (err) => (err instanceof Error ? err : Error("unexpected error"));
const safeImport = TE.tryCatchK((s) => import(s)),tError);
export const run= (globString) => pipe(
glob.sync(globString),
TE.traverseArray(safeImport),
async x => console.log(await x()),
)
//this call outputs
[{_tag: 'Left', left: Error [ERR_MODULE_NOT_FOUND]: Cannot find module...}]
but if I do:
const tError = (err) => (err instanceof Error ? err : Error("unexpected error"));
const safeImport = TE.tryCatchK((s) => import(s)),tError);
export const run= (globString) => pipe(
glob.sync(globString),
A.map(safeImport),
A.map(async x => console.log(await x())),
)
//this call outputs
[
{_tag: 'Left', left: Error [ERR_MODULE_NOT_FOUND]: Cannot find module...},
{_tag: 'Right', right:...},{_tag: 'Right', right:...},{_tag: 'Right', right:...},{_tag: 'Right', right:...}
]
//
I would like to flip the types but keeping both the left and the rights, any idea of what I am doing wrong?
Solution
So, the way to do this is by not using an TE.traverseArray
but T.traverseArray
:
const tError = (err) => (err instanceof Error ? err : Error("unexpected error"));
const safeImport = TE.tryCatchK((s) => import(s)),tError);
export const run= (globString) => pipe(
glob.sync(globString),
T.traverseArray(safeImport),
async x => console.log(await x()),
)
That will remove the Task wrapper and keep the eithers as elements of the array.
This Question was asked in StackOverflow by Ricardo Silva and Answered by Ricardo Silva It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.