简体   繁体   中英

fp-ts how to prevent traverse from short circuit on Either left?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM