1

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?

Ricardo Silva
  • 1,221
  • 9
  • 19

1 Answers1

1

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Ricardo Silva
  • 1,221
  • 9
  • 19