flingit 0.0.63 → 0.0.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (517) hide show
  1. package/dist/cli/commands/dev.d.ts.map +1 -1
  2. package/dist/cli/commands/dev.js +9 -5
  3. package/dist/cli/commands/dev.js.map +1 -1
  4. package/dist/cli/commands/workflow.d.ts +51 -0
  5. package/dist/cli/commands/workflow.d.ts.map +1 -0
  6. package/dist/cli/commands/workflow.js +479 -0
  7. package/dist/cli/commands/workflow.js.map +1 -0
  8. package/dist/cli/deploy/bundler.d.ts.map +1 -1
  9. package/dist/cli/deploy/bundler.js +17 -1
  10. package/dist/cli/deploy/bundler.js.map +1 -1
  11. package/dist/cli/index.d.ts.map +1 -1
  12. package/dist/cli/index.js +2 -0
  13. package/dist/cli/index.js.map +1 -1
  14. package/dist/index.d.ts +4 -1
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +3 -1
  17. package/dist/index.js.map +1 -1
  18. package/dist/runtime/entry.d.ts +7 -0
  19. package/dist/runtime/entry.d.ts.map +1 -1
  20. package/dist/runtime/entry.js +71 -2
  21. package/dist/runtime/entry.js.map +1 -1
  22. package/dist/types/workflow.d.ts +82 -0
  23. package/dist/types/workflow.d.ts.map +1 -0
  24. package/dist/types/workflow.js +8 -0
  25. package/dist/types/workflow.js.map +1 -0
  26. package/dist/worker-runtime/d1-event-store.d.ts +22 -0
  27. package/dist/worker-runtime/d1-event-store.d.ts.map +1 -0
  28. package/dist/worker-runtime/d1-event-store.js +227 -0
  29. package/dist/worker-runtime/d1-event-store.js.map +1 -0
  30. package/dist/worker-runtime/entry-extract.d.ts.map +1 -1
  31. package/dist/worker-runtime/entry-extract.js +6 -3
  32. package/dist/worker-runtime/entry-extract.js.map +1 -1
  33. package/dist/worker-runtime/entry.d.ts.map +1 -1
  34. package/dist/worker-runtime/entry.js +105 -3
  35. package/dist/worker-runtime/entry.js.map +1 -1
  36. package/dist/worker-runtime/index.d.ts +2 -0
  37. package/dist/worker-runtime/index.d.ts.map +1 -1
  38. package/dist/worker-runtime/index.js +5 -0
  39. package/dist/worker-runtime/index.js.map +1 -1
  40. package/dist/workflow/runtime.d.ts +56 -0
  41. package/dist/workflow/runtime.d.ts.map +1 -0
  42. package/dist/workflow/runtime.js +450 -0
  43. package/dist/workflow/runtime.js.map +1 -0
  44. package/node_modules/@glideapps/ts-necessities/LICENSE +21 -0
  45. package/node_modules/@glideapps/ts-necessities/README.md +16 -0
  46. package/node_modules/@glideapps/ts-necessities/dist/branded-strings.d.ts +39 -0
  47. package/node_modules/@glideapps/ts-necessities/dist/default-map.d.ts +53 -0
  48. package/node_modules/@glideapps/ts-necessities/dist/index.d.mts +113 -0
  49. package/node_modules/@glideapps/ts-necessities/dist/index.d.ts +113 -0
  50. package/node_modules/@glideapps/ts-necessities/dist/index.js +237 -0
  51. package/node_modules/@glideapps/ts-necessities/dist/index.mjs +189 -0
  52. package/node_modules/@glideapps/ts-necessities/package.json +52 -0
  53. package/node_modules/@rollup/rollup-linux-x64-gnu/README.md +3 -0
  54. package/node_modules/@rollup/rollup-linux-x64-gnu/package.json +25 -0
  55. package/node_modules/@rollup/rollup-linux-x64-gnu/rollup.linux-x64-gnu.node +0 -0
  56. package/node_modules/base64-js/LICENSE +21 -0
  57. package/node_modules/base64-js/README.md +34 -0
  58. package/node_modules/base64-js/base64js.min.js +1 -0
  59. package/node_modules/base64-js/index.d.ts +3 -0
  60. package/node_modules/base64-js/index.js +150 -0
  61. package/node_modules/base64-js/package.json +47 -0
  62. package/node_modules/better-sqlite3/LICENSE +21 -0
  63. package/node_modules/better-sqlite3/README.md +99 -0
  64. package/node_modules/better-sqlite3/binding.gyp +38 -0
  65. package/node_modules/better-sqlite3/deps/common.gypi +68 -0
  66. package/node_modules/better-sqlite3/deps/copy.js +31 -0
  67. package/node_modules/better-sqlite3/deps/defines.gypi +41 -0
  68. package/node_modules/better-sqlite3/deps/download.sh +122 -0
  69. package/node_modules/better-sqlite3/deps/patches/1208.patch +15 -0
  70. package/node_modules/better-sqlite3/deps/sqlite3/sqlite3.c +265994 -0
  71. package/node_modules/better-sqlite3/deps/sqlite3/sqlite3.h +13968 -0
  72. package/node_modules/better-sqlite3/deps/sqlite3/sqlite3ext.h +730 -0
  73. package/node_modules/better-sqlite3/deps/sqlite3.gyp +80 -0
  74. package/node_modules/better-sqlite3/deps/test_extension.c +21 -0
  75. package/node_modules/better-sqlite3/lib/database.js +90 -0
  76. package/node_modules/better-sqlite3/lib/index.js +3 -0
  77. package/node_modules/better-sqlite3/lib/methods/aggregate.js +43 -0
  78. package/node_modules/better-sqlite3/lib/methods/backup.js +67 -0
  79. package/node_modules/better-sqlite3/lib/methods/function.js +31 -0
  80. package/node_modules/better-sqlite3/lib/methods/inspect.js +7 -0
  81. package/node_modules/better-sqlite3/lib/methods/pragma.js +12 -0
  82. package/node_modules/better-sqlite3/lib/methods/serialize.js +16 -0
  83. package/node_modules/better-sqlite3/lib/methods/table.js +189 -0
  84. package/node_modules/better-sqlite3/lib/methods/transaction.js +78 -0
  85. package/node_modules/better-sqlite3/lib/methods/wrappers.js +54 -0
  86. package/node_modules/better-sqlite3/lib/sqlite-error.js +20 -0
  87. package/node_modules/better-sqlite3/lib/util.js +12 -0
  88. package/node_modules/better-sqlite3/package.json +59 -0
  89. package/node_modules/better-sqlite3/src/addon.cpp +47 -0
  90. package/node_modules/better-sqlite3/src/better_sqlite3.cpp +74 -0
  91. package/node_modules/better-sqlite3/src/objects/backup.cpp +120 -0
  92. package/node_modules/better-sqlite3/src/objects/backup.hpp +36 -0
  93. package/node_modules/better-sqlite3/src/objects/database.cpp +417 -0
  94. package/node_modules/better-sqlite3/src/objects/database.hpp +103 -0
  95. package/node_modules/better-sqlite3/src/objects/statement-iterator.cpp +113 -0
  96. package/node_modules/better-sqlite3/src/objects/statement-iterator.hpp +50 -0
  97. package/node_modules/better-sqlite3/src/objects/statement.cpp +383 -0
  98. package/node_modules/better-sqlite3/src/objects/statement.hpp +58 -0
  99. package/node_modules/better-sqlite3/src/util/bind-map.cpp +73 -0
  100. package/node_modules/better-sqlite3/src/util/binder.cpp +193 -0
  101. package/node_modules/better-sqlite3/src/util/constants.cpp +172 -0
  102. package/node_modules/better-sqlite3/src/util/custom-aggregate.cpp +121 -0
  103. package/node_modules/better-sqlite3/src/util/custom-function.cpp +59 -0
  104. package/node_modules/better-sqlite3/src/util/custom-table.cpp +409 -0
  105. package/node_modules/better-sqlite3/src/util/data-converter.cpp +17 -0
  106. package/node_modules/better-sqlite3/src/util/data.cpp +194 -0
  107. package/node_modules/better-sqlite3/src/util/helpers.cpp +109 -0
  108. package/node_modules/better-sqlite3/src/util/macros.cpp +83 -0
  109. package/node_modules/better-sqlite3/src/util/query-macros.cpp +71 -0
  110. package/node_modules/better-sqlite3/src/util/row-builder.cpp +49 -0
  111. package/node_modules/bindings/LICENSE.md +22 -0
  112. package/node_modules/bindings/README.md +98 -0
  113. package/node_modules/bindings/bindings.js +221 -0
  114. package/node_modules/bindings/package.json +28 -0
  115. package/node_modules/bl/.travis.yml +17 -0
  116. package/node_modules/bl/BufferList.js +396 -0
  117. package/node_modules/bl/LICENSE.md +13 -0
  118. package/node_modules/bl/README.md +247 -0
  119. package/node_modules/bl/bl.js +84 -0
  120. package/node_modules/bl/package.json +37 -0
  121. package/node_modules/bl/test/convert.js +21 -0
  122. package/node_modules/bl/test/indexOf.js +492 -0
  123. package/node_modules/bl/test/isBufferList.js +32 -0
  124. package/node_modules/bl/test/test.js +869 -0
  125. package/node_modules/buffer/AUTHORS.md +70 -0
  126. package/node_modules/buffer/LICENSE +21 -0
  127. package/node_modules/buffer/README.md +410 -0
  128. package/node_modules/buffer/index.d.ts +186 -0
  129. package/node_modules/buffer/index.js +1817 -0
  130. package/node_modules/buffer/package.json +96 -0
  131. package/node_modules/chownr/LICENSE +15 -0
  132. package/node_modules/chownr/README.md +3 -0
  133. package/node_modules/chownr/chownr.js +167 -0
  134. package/node_modules/chownr/package.json +29 -0
  135. package/node_modules/decompress-response/index.d.ts +22 -0
  136. package/node_modules/decompress-response/index.js +58 -0
  137. package/node_modules/decompress-response/license +9 -0
  138. package/node_modules/decompress-response/package.json +56 -0
  139. package/node_modules/decompress-response/readme.md +48 -0
  140. package/node_modules/deep-extend/LICENSE +20 -0
  141. package/node_modules/deep-extend/README.md +91 -0
  142. package/node_modules/deep-extend/index.js +1 -0
  143. package/node_modules/deep-extend/lib/deep-extend.js +150 -0
  144. package/node_modules/deep-extend/package.json +62 -0
  145. package/node_modules/detect-libc/LICENSE +201 -0
  146. package/node_modules/detect-libc/README.md +163 -0
  147. package/node_modules/detect-libc/index.d.ts +14 -0
  148. package/node_modules/detect-libc/lib/detect-libc.js +313 -0
  149. package/node_modules/detect-libc/lib/elf.js +39 -0
  150. package/node_modules/detect-libc/lib/filesystem.js +51 -0
  151. package/node_modules/detect-libc/lib/process.js +24 -0
  152. package/node_modules/detect-libc/package.json +44 -0
  153. package/node_modules/determined/LICENSE +18 -0
  154. package/node_modules/determined/README.md +317 -0
  155. package/node_modules/determined/dist/index.cjs +370 -0
  156. package/node_modules/determined/dist/index.d.cts +110 -0
  157. package/node_modules/determined/dist/index.d.ts +110 -0
  158. package/node_modules/determined/dist/index.js +332 -0
  159. package/node_modules/determined/package.json +45 -0
  160. package/node_modules/end-of-stream/LICENSE +21 -0
  161. package/node_modules/end-of-stream/README.md +54 -0
  162. package/node_modules/end-of-stream/index.js +96 -0
  163. package/node_modules/end-of-stream/package.json +37 -0
  164. package/node_modules/expand-template/.travis.yml +6 -0
  165. package/node_modules/expand-template/LICENSE +21 -0
  166. package/node_modules/expand-template/README.md +43 -0
  167. package/node_modules/expand-template/index.js +26 -0
  168. package/node_modules/expand-template/package.json +29 -0
  169. package/node_modules/expand-template/test.js +67 -0
  170. package/node_modules/file-uri-to-path/.npmignore +1 -0
  171. package/node_modules/file-uri-to-path/.travis.yml +30 -0
  172. package/node_modules/file-uri-to-path/History.md +21 -0
  173. package/node_modules/file-uri-to-path/LICENSE +20 -0
  174. package/node_modules/file-uri-to-path/README.md +74 -0
  175. package/node_modules/file-uri-to-path/index.d.ts +2 -0
  176. package/node_modules/file-uri-to-path/index.js +66 -0
  177. package/node_modules/file-uri-to-path/package.json +32 -0
  178. package/node_modules/file-uri-to-path/test/test.js +24 -0
  179. package/node_modules/file-uri-to-path/test/tests.json +13 -0
  180. package/node_modules/flingflow/.nvmrc +1 -0
  181. package/node_modules/flingflow/AGENTS.md +5 -0
  182. package/node_modules/flingflow/README.md +679 -0
  183. package/node_modules/flingflow/SPEC.md +554 -0
  184. package/node_modules/flingflow/TESTING.md +506 -0
  185. package/node_modules/flingflow/dist/backoff.d.ts +9 -0
  186. package/node_modules/flingflow/dist/backoff.js +14 -0
  187. package/node_modules/flingflow/dist/backoff.js.map +1 -0
  188. package/node_modules/flingflow/dist/clock.d.ts +13 -0
  189. package/node_modules/flingflow/dist/clock.js +21 -0
  190. package/node_modules/flingflow/dist/clock.js.map +1 -0
  191. package/node_modules/flingflow/dist/context.d.ts +8 -0
  192. package/node_modules/flingflow/dist/context.js +45 -0
  193. package/node_modules/flingflow/dist/context.js.map +1 -0
  194. package/node_modules/flingflow/dist/engine.d.ts +29 -0
  195. package/node_modules/flingflow/dist/engine.js +306 -0
  196. package/node_modules/flingflow/dist/engine.js.map +1 -0
  197. package/node_modules/flingflow/dist/index.d.ts +18 -0
  198. package/node_modules/flingflow/dist/index.js +14 -0
  199. package/node_modules/flingflow/dist/index.js.map +1 -0
  200. package/node_modules/flingflow/dist/recovery.d.ts +16 -0
  201. package/node_modules/flingflow/dist/recovery.js +118 -0
  202. package/node_modules/flingflow/dist/recovery.js.map +1 -0
  203. package/node_modules/flingflow/dist/registry.d.ts +24 -0
  204. package/node_modules/flingflow/dist/registry.js +29 -0
  205. package/node_modules/flingflow/dist/registry.js.map +1 -0
  206. package/node_modules/flingflow/dist/store-memory.d.ts +29 -0
  207. package/node_modules/flingflow/dist/store-memory.js +349 -0
  208. package/node_modules/flingflow/dist/store-memory.js.map +1 -0
  209. package/node_modules/flingflow/dist/store-sqlite.d.ts +30 -0
  210. package/node_modules/flingflow/dist/store-sqlite.js +400 -0
  211. package/node_modules/flingflow/dist/store-sqlite.js.map +1 -0
  212. package/node_modules/flingflow/dist/store.d.ts +39 -0
  213. package/node_modules/flingflow/dist/store.js +21 -0
  214. package/node_modules/flingflow/dist/store.js.map +1 -0
  215. package/node_modules/flingflow/dist/stress.d.ts +1 -0
  216. package/node_modules/flingflow/dist/stress.js +377 -0
  217. package/node_modules/flingflow/dist/stress.js.map +1 -0
  218. package/node_modules/flingflow/dist/transitions.d.ts +14 -0
  219. package/node_modules/flingflow/dist/transitions.js +28 -0
  220. package/node_modules/flingflow/dist/transitions.js.map +1 -0
  221. package/node_modules/flingflow/dist/types.d.ts +114 -0
  222. package/node_modules/flingflow/dist/types.js +10 -0
  223. package/node_modules/flingflow/dist/types.js.map +1 -0
  224. package/node_modules/flingflow/eslint.config.js +94 -0
  225. package/node_modules/flingflow/package.json +66 -0
  226. package/node_modules/flingflow/src/backoff.ts +14 -0
  227. package/node_modules/flingflow/src/clock.ts +29 -0
  228. package/node_modules/flingflow/src/context.ts +60 -0
  229. package/node_modules/flingflow/src/engine.ts +367 -0
  230. package/node_modules/flingflow/src/index.ts +52 -0
  231. package/node_modules/flingflow/src/recovery.ts +144 -0
  232. package/node_modules/flingflow/src/registry.ts +52 -0
  233. package/node_modules/flingflow/src/store-memory.ts +378 -0
  234. package/node_modules/flingflow/src/store-sqlite.ts +451 -0
  235. package/node_modules/flingflow/src/store.ts +55 -0
  236. package/node_modules/flingflow/src/stress.ts +423 -0
  237. package/node_modules/flingflow/src/transitions.ts +38 -0
  238. package/node_modules/flingflow/src/types.ts +84 -0
  239. package/node_modules/flingflow/test/backoff.test.ts +54 -0
  240. package/node_modules/flingflow/test/context.test.ts +94 -0
  241. package/node_modules/flingflow/test/engine.test.ts +362 -0
  242. package/node_modules/flingflow/test/fixtures.ts +58 -0
  243. package/node_modules/flingflow/test/recovery.test.ts +176 -0
  244. package/node_modules/flingflow/test/simulation.test.ts +395 -0
  245. package/node_modules/flingflow/test/store-conformance.ts +344 -0
  246. package/node_modules/flingflow/test/store-memory.test.ts +8 -0
  247. package/node_modules/flingflow/test/store-sqlite.test.ts +8 -0
  248. package/node_modules/flingflow/tsconfig.json +18 -0
  249. package/node_modules/flingflow/tsconfig.lint.json +5 -0
  250. package/node_modules/flingflow/tsconfig.typecheck.json +20 -0
  251. package/node_modules/flingflow/vitest.config.ts +10 -0
  252. package/node_modules/fs-constants/LICENSE +21 -0
  253. package/node_modules/fs-constants/README.md +26 -0
  254. package/node_modules/fs-constants/browser.js +1 -0
  255. package/node_modules/fs-constants/index.js +1 -0
  256. package/node_modules/fs-constants/package.json +19 -0
  257. package/node_modules/github-from-package/.travis.yml +4 -0
  258. package/node_modules/github-from-package/LICENSE +18 -0
  259. package/node_modules/github-from-package/example/package.json +8 -0
  260. package/node_modules/github-from-package/example/url.js +3 -0
  261. package/node_modules/github-from-package/index.js +17 -0
  262. package/node_modules/github-from-package/package.json +30 -0
  263. package/node_modules/github-from-package/readme.markdown +53 -0
  264. package/node_modules/github-from-package/test/a.json +8 -0
  265. package/node_modules/github-from-package/test/b.json +5 -0
  266. package/node_modules/github-from-package/test/c.json +5 -0
  267. package/node_modules/github-from-package/test/d.json +7 -0
  268. package/node_modules/github-from-package/test/e.json +5 -0
  269. package/node_modules/github-from-package/test/url.js +19 -0
  270. package/node_modules/ieee754/LICENSE +11 -0
  271. package/node_modules/ieee754/README.md +51 -0
  272. package/node_modules/ieee754/index.d.ts +10 -0
  273. package/node_modules/ieee754/index.js +85 -0
  274. package/node_modules/ieee754/package.json +52 -0
  275. package/node_modules/inherits/LICENSE +16 -0
  276. package/node_modules/inherits/README.md +42 -0
  277. package/node_modules/inherits/inherits.js +9 -0
  278. package/node_modules/inherits/inherits_browser.js +27 -0
  279. package/node_modules/inherits/package.json +29 -0
  280. package/node_modules/ini/LICENSE +15 -0
  281. package/node_modules/ini/README.md +102 -0
  282. package/node_modules/ini/ini.js +206 -0
  283. package/node_modules/ini/package.json +33 -0
  284. package/node_modules/mimic-response/index.d.ts +17 -0
  285. package/node_modules/mimic-response/index.js +77 -0
  286. package/node_modules/mimic-response/license +9 -0
  287. package/node_modules/mimic-response/package.json +42 -0
  288. package/node_modules/mimic-response/readme.md +78 -0
  289. package/node_modules/minimist/.eslintrc +29 -0
  290. package/node_modules/minimist/.github/FUNDING.yml +12 -0
  291. package/node_modules/minimist/.nycrc +14 -0
  292. package/node_modules/minimist/CHANGELOG.md +298 -0
  293. package/node_modules/minimist/LICENSE +18 -0
  294. package/node_modules/minimist/README.md +121 -0
  295. package/node_modules/minimist/example/parse.js +4 -0
  296. package/node_modules/minimist/index.js +263 -0
  297. package/node_modules/minimist/package.json +75 -0
  298. package/node_modules/minimist/test/all_bool.js +34 -0
  299. package/node_modules/minimist/test/bool.js +177 -0
  300. package/node_modules/minimist/test/dash.js +43 -0
  301. package/node_modules/minimist/test/default_bool.js +37 -0
  302. package/node_modules/minimist/test/dotted.js +24 -0
  303. package/node_modules/minimist/test/kv_short.js +32 -0
  304. package/node_modules/minimist/test/long.js +33 -0
  305. package/node_modules/minimist/test/num.js +38 -0
  306. package/node_modules/minimist/test/parse.js +209 -0
  307. package/node_modules/minimist/test/parse_modified.js +11 -0
  308. package/node_modules/minimist/test/proto.js +64 -0
  309. package/node_modules/minimist/test/short.js +69 -0
  310. package/node_modules/minimist/test/stop_early.js +17 -0
  311. package/node_modules/minimist/test/unknown.js +104 -0
  312. package/node_modules/minimist/test/whitespace.js +10 -0
  313. package/node_modules/mkdirp-classic/LICENSE +21 -0
  314. package/node_modules/mkdirp-classic/README.md +18 -0
  315. package/node_modules/mkdirp-classic/index.js +98 -0
  316. package/node_modules/mkdirp-classic/package.json +18 -0
  317. package/node_modules/napi-build-utils/.github/workflows/run-npm-tests.yml +31 -0
  318. package/node_modules/napi-build-utils/LICENSE +21 -0
  319. package/node_modules/napi-build-utils/README.md +52 -0
  320. package/node_modules/napi-build-utils/index.js +214 -0
  321. package/node_modules/napi-build-utils/index.md +0 -0
  322. package/node_modules/napi-build-utils/package.json +42 -0
  323. package/node_modules/neverthrow/LICENSE +22 -0
  324. package/node_modules/neverthrow/README.md +1683 -0
  325. package/node_modules/neverthrow/dist/index.cjs.js +510 -0
  326. package/node_modules/neverthrow/dist/index.d.ts +408 -0
  327. package/node_modules/neverthrow/dist/index.es.js +497 -0
  328. package/node_modules/neverthrow/package.json +64 -0
  329. package/node_modules/node-abi/LICENSE +21 -0
  330. package/node_modules/node-abi/README.md +54 -0
  331. package/node_modules/node-abi/abi_registry.json +425 -0
  332. package/node_modules/node-abi/index.js +179 -0
  333. package/node_modules/node-abi/package.json +45 -0
  334. package/node_modules/once/LICENSE +15 -0
  335. package/node_modules/once/README.md +79 -0
  336. package/node_modules/once/once.js +42 -0
  337. package/node_modules/once/package.json +33 -0
  338. package/node_modules/prebuild-install/CHANGELOG.md +131 -0
  339. package/node_modules/prebuild-install/CONTRIBUTING.md +6 -0
  340. package/node_modules/prebuild-install/LICENSE +21 -0
  341. package/node_modules/prebuild-install/README.md +163 -0
  342. package/node_modules/prebuild-install/asset.js +44 -0
  343. package/node_modules/prebuild-install/bin.js +78 -0
  344. package/node_modules/prebuild-install/download.js +142 -0
  345. package/node_modules/prebuild-install/error.js +14 -0
  346. package/node_modules/prebuild-install/help.txt +16 -0
  347. package/node_modules/prebuild-install/index.js +1 -0
  348. package/node_modules/prebuild-install/log.js +33 -0
  349. package/node_modules/prebuild-install/package.json +67 -0
  350. package/node_modules/prebuild-install/proxy.js +35 -0
  351. package/node_modules/prebuild-install/rc.js +64 -0
  352. package/node_modules/prebuild-install/util.js +143 -0
  353. package/node_modules/pump/.github/FUNDING.yml +2 -0
  354. package/node_modules/pump/.travis.yml +5 -0
  355. package/node_modules/pump/LICENSE +21 -0
  356. package/node_modules/pump/README.md +74 -0
  357. package/node_modules/pump/SECURITY.md +5 -0
  358. package/node_modules/pump/index.js +86 -0
  359. package/node_modules/pump/package.json +24 -0
  360. package/node_modules/pump/test-browser.js +66 -0
  361. package/node_modules/pump/test-node.js +53 -0
  362. package/node_modules/rc/LICENSE.APACHE2 +15 -0
  363. package/node_modules/rc/LICENSE.BSD +26 -0
  364. package/node_modules/rc/LICENSE.MIT +24 -0
  365. package/node_modules/rc/README.md +227 -0
  366. package/node_modules/rc/browser.js +7 -0
  367. package/node_modules/rc/cli.js +4 -0
  368. package/node_modules/rc/index.js +53 -0
  369. package/node_modules/rc/lib/utils.js +104 -0
  370. package/node_modules/rc/package.json +29 -0
  371. package/node_modules/rc/test/ini.js +16 -0
  372. package/node_modules/rc/test/nested-env-vars.js +50 -0
  373. package/node_modules/rc/test/test.js +59 -0
  374. package/node_modules/readable-stream/CONTRIBUTING.md +38 -0
  375. package/node_modules/readable-stream/GOVERNANCE.md +136 -0
  376. package/node_modules/readable-stream/LICENSE +47 -0
  377. package/node_modules/readable-stream/README.md +106 -0
  378. package/node_modules/readable-stream/errors-browser.js +127 -0
  379. package/node_modules/readable-stream/errors.js +116 -0
  380. package/node_modules/readable-stream/experimentalWarning.js +17 -0
  381. package/node_modules/readable-stream/lib/_stream_duplex.js +126 -0
  382. package/node_modules/readable-stream/lib/_stream_passthrough.js +37 -0
  383. package/node_modules/readable-stream/lib/_stream_readable.js +1027 -0
  384. package/node_modules/readable-stream/lib/_stream_transform.js +190 -0
  385. package/node_modules/readable-stream/lib/_stream_writable.js +641 -0
  386. package/node_modules/readable-stream/lib/internal/streams/async_iterator.js +180 -0
  387. package/node_modules/readable-stream/lib/internal/streams/buffer_list.js +183 -0
  388. package/node_modules/readable-stream/lib/internal/streams/destroy.js +96 -0
  389. package/node_modules/readable-stream/lib/internal/streams/end-of-stream.js +86 -0
  390. package/node_modules/readable-stream/lib/internal/streams/from-browser.js +3 -0
  391. package/node_modules/readable-stream/lib/internal/streams/from.js +52 -0
  392. package/node_modules/readable-stream/lib/internal/streams/pipeline.js +86 -0
  393. package/node_modules/readable-stream/lib/internal/streams/state.js +22 -0
  394. package/node_modules/readable-stream/lib/internal/streams/stream-browser.js +1 -0
  395. package/node_modules/readable-stream/lib/internal/streams/stream.js +1 -0
  396. package/node_modules/readable-stream/package.json +68 -0
  397. package/node_modules/readable-stream/readable-browser.js +9 -0
  398. package/node_modules/readable-stream/readable.js +16 -0
  399. package/node_modules/safe-buffer/LICENSE +21 -0
  400. package/node_modules/safe-buffer/README.md +584 -0
  401. package/node_modules/safe-buffer/index.d.ts +187 -0
  402. package/node_modules/safe-buffer/index.js +65 -0
  403. package/node_modules/safe-buffer/package.json +51 -0
  404. package/node_modules/semver/LICENSE +15 -0
  405. package/node_modules/semver/README.md +664 -0
  406. package/node_modules/semver/bin/semver.js +191 -0
  407. package/node_modules/semver/classes/comparator.js +143 -0
  408. package/node_modules/semver/classes/index.js +7 -0
  409. package/node_modules/semver/classes/range.js +557 -0
  410. package/node_modules/semver/classes/semver.js +333 -0
  411. package/node_modules/semver/functions/clean.js +8 -0
  412. package/node_modules/semver/functions/cmp.js +54 -0
  413. package/node_modules/semver/functions/coerce.js +62 -0
  414. package/node_modules/semver/functions/compare-build.js +9 -0
  415. package/node_modules/semver/functions/compare-loose.js +5 -0
  416. package/node_modules/semver/functions/compare.js +7 -0
  417. package/node_modules/semver/functions/diff.js +60 -0
  418. package/node_modules/semver/functions/eq.js +5 -0
  419. package/node_modules/semver/functions/gt.js +5 -0
  420. package/node_modules/semver/functions/gte.js +5 -0
  421. package/node_modules/semver/functions/inc.js +21 -0
  422. package/node_modules/semver/functions/lt.js +5 -0
  423. package/node_modules/semver/functions/lte.js +5 -0
  424. package/node_modules/semver/functions/major.js +5 -0
  425. package/node_modules/semver/functions/minor.js +5 -0
  426. package/node_modules/semver/functions/neq.js +5 -0
  427. package/node_modules/semver/functions/parse.js +18 -0
  428. package/node_modules/semver/functions/patch.js +5 -0
  429. package/node_modules/semver/functions/prerelease.js +8 -0
  430. package/node_modules/semver/functions/rcompare.js +5 -0
  431. package/node_modules/semver/functions/rsort.js +5 -0
  432. package/node_modules/semver/functions/satisfies.js +12 -0
  433. package/node_modules/semver/functions/sort.js +5 -0
  434. package/node_modules/semver/functions/valid.js +8 -0
  435. package/node_modules/semver/index.js +91 -0
  436. package/node_modules/semver/internal/constants.js +37 -0
  437. package/node_modules/semver/internal/debug.js +11 -0
  438. package/node_modules/semver/internal/identifiers.js +29 -0
  439. package/node_modules/semver/internal/lrucache.js +42 -0
  440. package/node_modules/semver/internal/parse-options.js +17 -0
  441. package/node_modules/semver/internal/re.js +223 -0
  442. package/node_modules/semver/package.json +78 -0
  443. package/node_modules/semver/preload.js +4 -0
  444. package/node_modules/semver/range.bnf +16 -0
  445. package/node_modules/semver/ranges/gtr.js +6 -0
  446. package/node_modules/semver/ranges/intersects.js +9 -0
  447. package/node_modules/semver/ranges/ltr.js +6 -0
  448. package/node_modules/semver/ranges/max-satisfying.js +27 -0
  449. package/node_modules/semver/ranges/min-satisfying.js +26 -0
  450. package/node_modules/semver/ranges/min-version.js +63 -0
  451. package/node_modules/semver/ranges/outside.js +82 -0
  452. package/node_modules/semver/ranges/simplify.js +49 -0
  453. package/node_modules/semver/ranges/subset.js +249 -0
  454. package/node_modules/semver/ranges/to-comparators.js +10 -0
  455. package/node_modules/semver/ranges/valid.js +13 -0
  456. package/node_modules/simple-concat/.travis.yml +3 -0
  457. package/node_modules/simple-concat/LICENSE +20 -0
  458. package/node_modules/simple-concat/README.md +44 -0
  459. package/node_modules/simple-concat/index.js +15 -0
  460. package/node_modules/simple-concat/package.json +47 -0
  461. package/node_modules/simple-concat/test/basic.js +41 -0
  462. package/node_modules/simple-get/.github/dependabot.yml +15 -0
  463. package/node_modules/simple-get/.github/workflows/ci.yml +23 -0
  464. package/node_modules/simple-get/LICENSE +20 -0
  465. package/node_modules/simple-get/README.md +333 -0
  466. package/node_modules/simple-get/index.js +108 -0
  467. package/node_modules/simple-get/package.json +67 -0
  468. package/node_modules/string_decoder/LICENSE +48 -0
  469. package/node_modules/string_decoder/README.md +47 -0
  470. package/node_modules/string_decoder/lib/string_decoder.js +296 -0
  471. package/node_modules/string_decoder/package.json +34 -0
  472. package/node_modules/strip-json-comments/index.js +70 -0
  473. package/node_modules/strip-json-comments/license +21 -0
  474. package/node_modules/strip-json-comments/package.json +42 -0
  475. package/node_modules/strip-json-comments/readme.md +64 -0
  476. package/node_modules/tar-fs/.travis.yml +6 -0
  477. package/node_modules/tar-fs/LICENSE +21 -0
  478. package/node_modules/tar-fs/README.md +165 -0
  479. package/node_modules/tar-fs/index.js +363 -0
  480. package/node_modules/tar-fs/package.json +41 -0
  481. package/node_modules/tar-fs/test/fixtures/a/hello.txt +1 -0
  482. package/node_modules/tar-fs/test/fixtures/b/a/test.txt +1 -0
  483. package/node_modules/tar-fs/test/fixtures/d/file1 +0 -0
  484. package/node_modules/tar-fs/test/fixtures/d/file2 +0 -0
  485. package/node_modules/tar-fs/test/fixtures/d/sub-dir/file5 +0 -0
  486. package/node_modules/tar-fs/test/fixtures/d/sub-files/file3 +0 -0
  487. package/node_modules/tar-fs/test/fixtures/d/sub-files/file4 +0 -0
  488. package/node_modules/tar-fs/test/fixtures/e/directory/.ignore +0 -0
  489. package/node_modules/tar-fs/test/fixtures/e/file +0 -0
  490. package/node_modules/tar-fs/test/fixtures/invalid.tar +0 -0
  491. package/node_modules/tar-fs/test/index.js +346 -0
  492. package/node_modules/tar-stream/LICENSE +21 -0
  493. package/node_modules/tar-stream/README.md +168 -0
  494. package/node_modules/tar-stream/extract.js +257 -0
  495. package/node_modules/tar-stream/headers.js +295 -0
  496. package/node_modules/tar-stream/index.js +2 -0
  497. package/node_modules/tar-stream/pack.js +255 -0
  498. package/node_modules/tar-stream/package.json +58 -0
  499. package/node_modules/tar-stream/sandbox.js +11 -0
  500. package/node_modules/tunnel-agent/LICENSE +55 -0
  501. package/node_modules/tunnel-agent/README.md +4 -0
  502. package/node_modules/tunnel-agent/index.js +244 -0
  503. package/node_modules/tunnel-agent/package.json +22 -0
  504. package/node_modules/util-deprecate/History.md +16 -0
  505. package/node_modules/util-deprecate/LICENSE +24 -0
  506. package/node_modules/util-deprecate/README.md +53 -0
  507. package/node_modules/util-deprecate/browser.js +67 -0
  508. package/node_modules/util-deprecate/node.js +6 -0
  509. package/node_modules/util-deprecate/package.json +27 -0
  510. package/node_modules/wrappy/LICENSE +15 -0
  511. package/node_modules/wrappy/README.md +36 -0
  512. package/node_modules/wrappy/package.json +29 -0
  513. package/node_modules/wrappy/wrappy.js +33 -0
  514. package/package.json +12 -2
  515. package/templates/default/dot-claude/skills/fling/.hash +1 -1
  516. package/templates/default/dot-claude/skills/fling/SKILL.md +56 -29
  517. package/templates/default/dot-claude/skills/fling/references/WORKFLOWS.md +368 -0
@@ -0,0 +1,1683 @@
1
+ # NeverThrow 🙅
2
+
3
+ [![GitHub Workflow Status](https://github.com/supermacro/neverthrow/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/supermacro/neverthrow/actions)
4
+
5
+ ## Description
6
+
7
+ Encode failure into your program.
8
+
9
+ This package contains a `Result` type that represents either success (`Ok`) or failure (`Err`).
10
+
11
+ For asynchronous tasks, `neverthrow` offers a `ResultAsync` class which wraps a `Promise<Result<T, E>>` and gives you the same level of expressivity and control as a regular `Result<T, E>`.
12
+
13
+ `ResultAsync` is `thenable` meaning it **behaves exactly like a native `Promise<Result>`** ... except you have access to the same methods that `Result` provides without having to `await` or `.then` the promise! Check out [the wiki](https://github.com/supermacro/neverthrow/wiki/Basic-Usage-Examples#asynchronous-api) for examples and best practices.
14
+
15
+ > Need to see real-life examples of how to leverage this package for error handling? See this repo: https://github.com/parlez-vous/server
16
+
17
+ <div id="toc"></div>
18
+
19
+ ## Table Of Contents
20
+
21
+ * [Installation](#installation)
22
+ * [Recommended: Use `eslint-plugin-neverthrow`](#recommended-use-eslint-plugin-neverthrow)
23
+ * [Top-Level API](#top-level-api)
24
+ * [API Documentation](#api-documentation)
25
+ + [Synchronous API (`Result`)](#synchronous-api-result)
26
+ - [`ok`](#ok)
27
+ - [`err`](#err)
28
+ - [`Result.isOk` (method)](#resultisok-method)
29
+ - [`Result.isErr` (method)](#resultiserr-method)
30
+ - [`Result.map` (method)](#resultmap-method)
31
+ - [`Result.mapErr` (method)](#resultmaperr-method)
32
+ - [`Result.unwrapOr` (method)](#resultunwrapor-method)
33
+ - [`Result.andThen` (method)](#resultandthen-method)
34
+ - [`Result.asyncAndThen` (method)](#resultasyncandthen-method)
35
+ - [`Result.orElse` (method)](#resultorelse-method)
36
+ - [`Result.match` (method)](#resultmatch-method)
37
+ - [`Result.asyncMap` (method)](#resultasyncmap-method)
38
+ - [`Result.andTee` (method)](#resultandtee-method)
39
+ - [`Result.orTee` (method)](#resultortee-method)
40
+ - [`Result.andThrough` (method)](#resultandthrough-method)
41
+ - [`Result.asyncAndThrough` (method)](#resultasyncandthrough-method)
42
+ - [`Result.fromThrowable` (static class method)](#resultfromthrowable-static-class-method)
43
+ - [`Result.combine` (static class method)](#resultcombine-static-class-method)
44
+ - [`Result.combineWithAllErrors` (static class method)](#resultcombinewithallerrors-static-class-method)
45
+ - [`Result.safeUnwrap()`](#resultsafeunwrap)
46
+ + [Asynchronous API (`ResultAsync`)](#asynchronous-api-resultasync)
47
+ - [`okAsync`](#okasync)
48
+ - [`errAsync`](#errasync)
49
+ - [`ResultAsync.fromThrowable` (static class method)](#resultasyncfromthrowable-static-class-method)
50
+ - [`ResultAsync.fromPromise` (static class method)](#resultasyncfrompromise-static-class-method)
51
+ - [`ResultAsync.fromSafePromise` (static class method)](#resultasyncfromsafepromise-static-class-method)
52
+ - [`ResultAsync.map` (method)](#resultasyncmap-method)
53
+ - [`ResultAsync.mapErr` (method)](#resultasyncmaperr-method)
54
+ - [`ResultAsync.unwrapOr` (method)](#resultasyncunwrapor-method)
55
+ - [`ResultAsync.andThen` (method)](#resultasyncandthen-method)
56
+ - [`ResultAsync.orElse` (method)](#resultasyncorelse-method)
57
+ - [`ResultAsync.match` (method)](#resultasyncmatch-method)
58
+ - [`ResultAsync.andTee` (method)](#resultasyncandtee-method)
59
+ - [`ResultAsync.orTee` (method)](#resultasyncortee-method)
60
+ - [`ResultAsync.andThrough` (method)](#resultasyncandthrough-method)
61
+ - [`ResultAsync.combine` (static class method)](#resultasynccombine-static-class-method)
62
+ - [`ResultAsync.combineWithAllErrors` (static class method)](#resultasynccombinewithallerrors-static-class-method)
63
+ - [`ResultAsync.safeUnwrap()`](#resultasyncsafeunwrap)
64
+ + [Utilities](#utilities)
65
+ - [`fromThrowable`](#fromthrowable)
66
+ - [`fromAsyncThrowable`](#fromasyncthrowable)
67
+ - [`fromPromise`](#frompromise)
68
+ - [`fromSafePromise`](#fromsafepromise)
69
+ - [`safeTry`](#safetry)
70
+ + [Testing](#testing)
71
+ * [A note on the Package Name](#a-note-on-the-package-name)
72
+
73
+ ## Installation
74
+
75
+ ```sh
76
+ > npm install neverthrow
77
+ ```
78
+
79
+ ## Recommended: Use `eslint-plugin-neverthrow`
80
+
81
+ As part of `neverthrow`s [bounty program](https://github.com/supermacro/neverthrow/issues/314), user [mdbetancourt](https://github.com/mdbetancourt) created [`eslint-plugin-neverthrow`](https://github.com/mdbetancourt/eslint-plugin-neverthrow) to ensure that errors are not gone unhandled.
82
+
83
+ Install by running:
84
+
85
+ ```sh
86
+ > npm install eslint-plugin-neverthrow
87
+ ```
88
+
89
+ With `eslint-plugin-neverthrow`, you are forced to consume the result in one of the following three ways:
90
+
91
+ - Calling `.match`
92
+ - Calling `.unwrapOr`
93
+ - Calling `._unsafeUnwrap`
94
+
95
+ This ensures that you're explicitly handling the error of your `Result`.
96
+
97
+ This plugin is essentially a porting of Rust's [`must-use`](https://doc.rust-lang.org/std/result/#results-must-be-used) attribute.
98
+
99
+
100
+ ## Top-Level API
101
+
102
+ `neverthrow` exposes the following:
103
+
104
+ - `ok` convenience function to create an `Ok` variant of `Result`
105
+ - `err` convenience function to create an `Err` variant of `Result`
106
+ - `Ok` class and type
107
+ - `Err` class and type
108
+ - `Result` Type as well as namespace / object from which to call [`Result.fromThrowable`](#resultfromthrowable-static-class-method), [Result.combine](#resultcombine-static-class-method).
109
+ - `ResultAsync` class
110
+ - `okAsync` convenience function to create a `ResultAsync` containing an `Ok` type `Result`
111
+ - `errAsync` convenience function to create a `ResultAsync` containing an `Err` type `Result`
112
+
113
+ ```typescript
114
+ import {
115
+ ok,
116
+ Ok,
117
+ err,
118
+ Err,
119
+ Result,
120
+ okAsync,
121
+ errAsync,
122
+ ResultAsync,
123
+ fromAsyncThrowable,
124
+ fromThrowable,
125
+ fromPromise,
126
+ fromSafePromise,
127
+ safeTry,
128
+ } from 'neverthrow'
129
+ ```
130
+
131
+ ---
132
+
133
+ **Check out the [wiki](https://github.com/supermacro/neverthrow/wiki) for help on how to make the most of `neverthrow`.**
134
+
135
+ If you find this package useful, please consider [sponsoring me](https://github.com/sponsors/supermacro/) or simply [buying me a coffee](https://ko-fi.com/gdelgado)!
136
+
137
+ ---
138
+
139
+ ## API Documentation
140
+
141
+ ### Synchronous API (`Result`)
142
+
143
+ #### `ok`
144
+
145
+ Constructs an `Ok` variant of `Result`
146
+
147
+ **Signature:**
148
+
149
+ ```typescript
150
+ ok<T, E>(value: T): Ok<T, E> { ... }
151
+ ```
152
+
153
+ **Example:**
154
+
155
+ ```typescript
156
+ import { ok } from 'neverthrow'
157
+
158
+ const myResult = ok({ myData: 'test' }) // instance of `Ok`
159
+
160
+ myResult.isOk() // true
161
+ myResult.isErr() // false
162
+ ```
163
+
164
+ [⬆️ Back to top](#toc)
165
+
166
+ ---
167
+
168
+ #### `err`
169
+
170
+ Constructs an `Err` variant of `Result`
171
+
172
+ **Signature:**
173
+
174
+ ```typescript
175
+ err<T, E>(error: E): Err<T, E> { ... }
176
+ ```
177
+
178
+ **Example:**
179
+
180
+ ```typescript
181
+ import { err } from 'neverthrow'
182
+
183
+ const myResult = err('Oh noooo') // instance of `Err`
184
+
185
+ myResult.isOk() // false
186
+ myResult.isErr() // true
187
+ ```
188
+
189
+ [⬆️ Back to top](#toc)
190
+
191
+ ---
192
+
193
+ #### `Result.isOk` (method)
194
+
195
+ Returns `true` if the result is an `Ok` variant
196
+
197
+ **Signature:**
198
+
199
+ ```typescript
200
+ isOk(): boolean { ... }
201
+ ```
202
+
203
+ [⬆️ Back to top](#toc)
204
+
205
+ ---
206
+
207
+ #### `Result.isErr` (method)
208
+
209
+ Returns `true` if the result is an `Err` variant
210
+
211
+ **Signature**:
212
+
213
+ ```typescript
214
+ isErr(): boolean { ... }
215
+ ```
216
+
217
+ [⬆️ Back to top](#toc)
218
+
219
+ ---
220
+
221
+ #### `Result.map` (method)
222
+
223
+ Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value untouched.
224
+
225
+ This function can be used to compose the results of two functions.
226
+
227
+ **Signature:**
228
+
229
+ ```typescript
230
+ class Result<T, E> {
231
+ map<U>(callback: (value: T) => U): Result<U, E> { ... }
232
+ }
233
+ ```
234
+
235
+ **Example**:
236
+
237
+ ```typescript
238
+ import { getLines } from 'imaginary-parser'
239
+ // ^ assume getLines has the following signature:
240
+ // getLines(str: string): Result<Array<string>, Error>
241
+
242
+ // since the formatting is deemed correct by `getLines`
243
+ // then it means that `linesResult` is an Ok
244
+ // containing an Array of strings for each line of code
245
+ const linesResult = getLines('1\n2\n3\n4\n')
246
+
247
+ // this Result now has a Array<number> inside it
248
+ const newResult = linesResult.map(
249
+ (arr: Array<string>) => arr.map(parseInt)
250
+ )
251
+
252
+ newResult.isOk() // true
253
+ ```
254
+
255
+ [⬆️ Back to top](#toc)
256
+
257
+ ---
258
+
259
+ #### `Result.mapErr` (method)
260
+
261
+ Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value untouched.
262
+
263
+ This function can be used to pass through a successful result while handling an error.
264
+
265
+ **Signature:**
266
+
267
+ ```typescript
268
+ class Result<T, E> {
269
+ mapErr<F>(callback: (error: E) => F): Result<T, F> { ... }
270
+ }
271
+ ```
272
+
273
+ **Example**:
274
+
275
+ ```typescript
276
+ import { parseHeaders } from 'imaginary-http-parser'
277
+ // imagine that parseHeaders has the following signature:
278
+ // parseHeaders(raw: string): Result<SomeKeyValueMap, ParseError>
279
+
280
+ const rawHeaders = 'nonsensical gibberish and badly formatted stuff'
281
+
282
+ const parseResult = parseHeaders(rawHeaders)
283
+
284
+ parseResult.mapErr(parseError => {
285
+ res.status(400).json({
286
+ error: parseError
287
+ })
288
+ })
289
+
290
+ parseResult.isErr() // true
291
+ ```
292
+
293
+ [⬆️ Back to top](#toc)
294
+
295
+ ---
296
+
297
+ #### `Result.unwrapOr` (method)
298
+
299
+ Unwrap the `Ok` value, or return the default if there is an `Err`
300
+
301
+ **Signature:**
302
+
303
+ ```typescript
304
+ class Result<T, E> {
305
+ unwrapOr<T>(value: T): T { ... }
306
+ }
307
+ ```
308
+
309
+ **Example**:
310
+
311
+ ```typescript
312
+ const myResult = err('Oh noooo')
313
+
314
+ const multiply = (value: number): number => value * 2
315
+
316
+ const unwrapped: number = myResult.map(multiply).unwrapOr(10)
317
+ ```
318
+
319
+ [⬆️ Back to top](#toc)
320
+
321
+ ---
322
+
323
+ #### `Result.andThen` (method)
324
+
325
+ Same idea as `map` above. Except you must return a new `Result`.
326
+
327
+ The returned value will be a `Result`. As of `v4.1.0-beta`, you are able to return distinct error types (see signature below). Prior to `v4.1.0-beta`, the error type could not be distinct.
328
+
329
+ This is useful for when you need to do a subsequent computation using the inner `T` value, but that computation might fail.
330
+
331
+ Additionally, `andThen` is really useful as a tool to flatten a `Result<Result<A, E2>, E1>` into a `Result<A, E2>` (see example below).
332
+
333
+ **Signature:**
334
+
335
+ ```typescript
336
+ class Result<T, E> {
337
+ // Note that the latest version lets you return distinct errors as well.
338
+ // If the error types (E and F) are the same (like `string | string`)
339
+ // then they will be merged into one type (`string`)
340
+ andThen<U, F>(
341
+ callback: (value: T) => Result<U, F>
342
+ ): Result<U, E | F> { ... }
343
+ }
344
+ ```
345
+
346
+ **Example 1: Chaining Results**
347
+
348
+ ```typescript
349
+ import { err, ok } from 'neverthrow'
350
+
351
+ const sq = (n: number): Result<number, number> => ok(n ** 2)
352
+
353
+ ok(2)
354
+ .andThen(sq)
355
+ .andThen(sq) // Ok(16)
356
+
357
+ ok(2)
358
+ .andThen(sq)
359
+ .andThen(err) // Err(4)
360
+
361
+ ok(2)
362
+ .andThen(err)
363
+ .andThen(sq) // Err(2)
364
+
365
+ err(3)
366
+ .andThen(sq)
367
+ .andThen(sq) // Err(3)
368
+ ```
369
+
370
+ **Example 2: Flattening Nested Results**
371
+
372
+ ```typescript
373
+ // It's common to have nested Results
374
+ const nested = ok(ok(1234))
375
+
376
+ // notNested is a Ok(1234)
377
+ const notNested = nested.andThen((innerResult) => innerResult)
378
+ ```
379
+
380
+ [⬆️ Back to top](#toc)
381
+
382
+ ---
383
+
384
+ #### `Result.asyncAndThen` (method)
385
+
386
+ Same idea as [`andThen` above](#resultandthen-method), except you must return a new `ResultAsync`.
387
+
388
+ The returned value will be a `ResultAsync`.
389
+
390
+ **Signature:**
391
+
392
+ ```typescript
393
+ class Result<T, E> {
394
+ asyncAndThen<U, F>(
395
+ callback: (value: T) => ResultAsync<U, F>
396
+ ): ResultAsync<U, E | F> { ... }
397
+ }
398
+ ```
399
+
400
+ [⬆️ Back to top](#toc)
401
+
402
+ ---
403
+
404
+ #### `Result.orElse` (method)
405
+
406
+ Takes an `Err` value and maps it to a `Result<T, SomeNewType>`. This is useful for error recovery.
407
+
408
+ **Signature:**
409
+
410
+ ```typescript
411
+ class Result<T, E> {
412
+ orElse<U, A>(
413
+ callback: (error: E) => Result<U, A>
414
+ ): Result<U | T, A> { ... }
415
+ }
416
+ ```
417
+
418
+ **Example:**
419
+
420
+ ```typescript
421
+ enum DatabaseError {
422
+ PoolExhausted = 'PoolExhausted',
423
+ NotFound = 'NotFound',
424
+ }
425
+
426
+ const dbQueryResult: Result<string, DatabaseError> = err(DatabaseError.NotFound)
427
+
428
+ const updatedQueryResult = dbQueryResult.orElse((dbError) =>
429
+ dbError === DatabaseError.NotFound
430
+ ? ok('User does not exist') // error recovery branch: ok() must be called with a value of type string
431
+ //
432
+ //
433
+ // err() can be called with a value of any new type that you want
434
+ // it could also be called with the same error value
435
+ //
436
+ // err(dbError)
437
+ : err(500)
438
+ )
439
+ ```
440
+
441
+ [⬆️ Back to top](#toc)
442
+
443
+ ---
444
+
445
+ #### `Result.match` (method)
446
+
447
+ Given 2 functions (one for the `Ok` variant and one for the `Err` variant) execute the function that matches the `Result` variant.
448
+
449
+ Match callbacks do not necessitate to return a `Result`, however you can return a `Result` if you want to.
450
+
451
+ **Signature:**
452
+
453
+ ```typescript
454
+ class Result<T, E> {
455
+ match<A, B = A>(
456
+ okCallback: (value: T) => A,
457
+ errorCallback: (error: E) => B
458
+ ): A | B => { ... }
459
+ }
460
+ ```
461
+
462
+ `match` is like chaining `map` and `mapErr`, with the distinction that with `match` both functions must have the same return type.
463
+ The differences between `match` and chaining `map` and `mapErr` are that:
464
+ - with `match` both functions must have the same return type `A`
465
+ - `match` unwraps the `Result<T, E>` into an `A` (the match functions' return type)
466
+ - This makes no difference if you are performing side effects only
467
+
468
+ **Example:**
469
+
470
+ ```typescript
471
+ // map/mapErr api
472
+ // note that you DON'T have to append mapErr
473
+ // after map which means that you are not required to do
474
+ // error handling
475
+ computationThatMightFail().map(console.log).mapErr(console.error)
476
+
477
+ // match api
478
+ // works exactly the same as above since both callbacks
479
+ // only perform side effects,
480
+ // except, now you HAVE to do error handling :)
481
+ computationThatMightFail().match(console.log, console.error)
482
+
483
+ // Returning values
484
+ const attempt = computationThatMightFail()
485
+ .map((str) => str.toUpperCase())
486
+ .mapErr((err) => `Error: ${err}`)
487
+ // `attempt` is of type `Result<string, string>`
488
+
489
+ const answer = computationThatMightFail().match(
490
+ (str) => str.toUpperCase(),
491
+ (err) => `Error: ${err}`
492
+ )
493
+ // `answer` is of type `string`
494
+ ```
495
+
496
+ If you don't use the error parameter in your match callback then `match` is equivalent to chaining `map` with `unwrapOr`:
497
+ ```ts
498
+ const answer = computationThatMightFail().match(
499
+ (str) => str.toUpperCase(),
500
+ () => 'ComputationError'
501
+ )
502
+ // `answer` is of type `string`
503
+
504
+ const answer = computationThatMightFail()
505
+ .map((str) => str.toUpperCase())
506
+ .unwrapOr('ComputationError')
507
+ ```
508
+
509
+
510
+ [⬆️ Back to top](#toc)
511
+
512
+ ---
513
+
514
+ #### `Result.asyncMap` (method)
515
+
516
+ Similar to `map` except for two things:
517
+
518
+ - the mapping function must return a `Promise`
519
+ - asyncMap returns a `ResultAsync`
520
+
521
+ You can then chain the result of `asyncMap` using the `ResultAsync` apis (like `map`, `mapErr`, `andThen`, etc.)
522
+
523
+ **Signature:**
524
+
525
+ ```typescript
526
+ class Result<T, E> {
527
+ asyncMap<U>(
528
+ callback: (value: T) => Promise<U>
529
+ ): ResultAsync<U, E> { ... }
530
+ }
531
+ ```
532
+
533
+ **Example:**
534
+
535
+ ```typescript
536
+ import { parseHeaders } from 'imaginary-http-parser'
537
+ // imagine that parseHeaders has the following signature:
538
+ // parseHeaders(raw: string): Result<SomeKeyValueMap, ParseError>
539
+
540
+ const asyncRes = parseHeaders(rawHeader)
541
+ .map(headerKvMap => headerKvMap.Authorization)
542
+ .asyncMap(findUserInDatabase)
543
+ ```
544
+
545
+ Note that in the above example if `parseHeaders` returns an `Err` then `.map` and `.asyncMap` will not be invoked, and `asyncRes` variable will resolve to an `Err` when turned into a `Result` using `await` or `.then()`.
546
+
547
+ [⬆️ Back to top](#toc)
548
+
549
+ ---
550
+
551
+ #### `Result.andTee` (method)
552
+
553
+ Takes a `Result<T, E>` and lets the original `Result<T, E>` pass through regardless the result of the passed-in function.
554
+ This is a handy way to handle side effects whose failure or success should not affect your main logics such as logging.
555
+
556
+ **Signature:**
557
+
558
+ ```typescript
559
+ class Result<T, E> {
560
+ andTee(
561
+ callback: (value: T) => unknown
562
+ ): Result<T, E> { ... }
563
+ }
564
+ ```
565
+
566
+ **Example:**
567
+
568
+ ```typescript
569
+ import { parseUserInput } from 'imaginary-parser'
570
+ import { logUser } from 'imaginary-logger'
571
+ import { insertUser } from 'imaginary-database'
572
+
573
+ // ^ assume parseUserInput, logUser and insertUser have the following signatures:
574
+ // parseUserInput(input: RequestData): Result<User, ParseError>
575
+ // logUser(user: User): Result<void, LogError>
576
+ // insertUser(user: User): ResultAsync<void, InsertError>
577
+ // Note logUser returns void upon success but insertUser takes User type.
578
+
579
+ const resAsync = parseUserInput(userInput)
580
+ .andTee(logUser)
581
+ .asyncAndThen(insertUser)
582
+
583
+ // Note no LogError shows up in the Result type
584
+ resAsync.then((res: Result<void, ParseError | InsertError>) => {e
585
+ if(res.isErr()){
586
+ console.log("Oops, at least one step failed", res.error)
587
+ }
588
+ else{
589
+ console.log("User input has been parsed and inserted successfully.")
590
+ }
591
+ }))
592
+ ```
593
+
594
+ [⬆️ Back to top](#toc)
595
+
596
+ ---
597
+
598
+ #### `Result.orTee` (method)
599
+
600
+ Like `andTee` for the error track. Takes a `Result<T, E>` and lets the `Err` value pass through regardless the result of the passed-in function.
601
+ This is a handy way to handle side effects whose failure or success should not affect your main logics such as logging.
602
+
603
+ **Signature:**
604
+
605
+ ```typescript
606
+ class Result<T, E> {
607
+ orTee(
608
+ callback: (value: E) => unknown
609
+ ): Result<T, E> { ... }
610
+ }
611
+ ```
612
+
613
+ **Example:**
614
+
615
+ ```typescript
616
+ import { parseUserInput } from 'imaginary-parser'
617
+ import { logParseError } from 'imaginary-logger'
618
+ import { insertUser } from 'imaginary-database'
619
+
620
+ // ^ assume parseUserInput, logParseError and insertUser have the following signatures:
621
+ // parseUserInput(input: RequestData): Result<User, ParseError>
622
+ // logParseError(parseError: ParseError): Result<void, LogError>
623
+ // insertUser(user: User): ResultAsync<void, InsertError>
624
+ // Note logParseError returns void upon success but insertUser takes User type.
625
+
626
+ const resAsync = parseUserInput(userInput)
627
+ .orTee(logParseError)
628
+ .asyncAndThen(insertUser)
629
+
630
+ // Note no LogError shows up in the Result type
631
+ resAsync.then((res: Result<void, ParseError | InsertError>) => {e
632
+ if(res.isErr()){
633
+ console.log("Oops, at least one step failed", res.error)
634
+ }
635
+ else{
636
+ console.log("User input has been parsed and inserted successfully.")
637
+ }
638
+ }))
639
+ ```
640
+
641
+ [⬆️ Back to top](#toc)
642
+
643
+ ---
644
+
645
+ #### `Result.andThrough` (method)
646
+
647
+ Similar to `andTee` except for:
648
+
649
+ - when there is an error from the passed-in function, that error will be passed along.
650
+
651
+ **Signature:**
652
+
653
+ ```typescript
654
+ class Result<T, E> {
655
+ andThrough<F>(
656
+ callback: (value: T) => Result<unknown, F>
657
+ ): Result<T, E | F> { ... }
658
+ }
659
+ ```
660
+
661
+ **Example:**
662
+
663
+ ```typescript
664
+ import { parseUserInput } from 'imaginary-parser'
665
+ import { validateUser } from 'imaginary-validator'
666
+ import { insertUser } from 'imaginary-database'
667
+
668
+ // ^ assume parseUseInput, validateUser and insertUser have the following signatures:
669
+ // parseUserInput(input: RequestData): Result<User, ParseError>
670
+ // validateUser(user: User): Result<void, ValidateError>
671
+ // insertUser(user: User): ResultAsync<void, InsertError>
672
+ // Note validateUser returns void upon success but insertUser takes User type.
673
+
674
+ const resAsync = parseUserInput(userInput)
675
+ .andThrough(validateUser)
676
+ .asyncAndThen(insertUser)
677
+
678
+ resAsync.then((res: Result<void, ParseErro | ValidateError | InsertError>) => {e
679
+ if(res.isErr()){
680
+ console.log("Oops, at least one step failed", res.error)
681
+ }
682
+ else{
683
+ console.log("User input has been parsed, validated, inserted successfully.")
684
+ }
685
+ }))
686
+ ```
687
+
688
+ [⬆️ Back to top](#toc)
689
+
690
+ ---
691
+
692
+ #### `Result.asyncAndThrough` (method)
693
+
694
+ Similar to `andThrough` except you must return a ResultAsync.
695
+
696
+ You can then chain the result of `asyncAndThrough` using the `ResultAsync` apis (like `map`, `mapErr`, `andThen`, etc.)
697
+
698
+ **Signature:**
699
+
700
+ ```typescript
701
+ import { parseUserInput } from 'imaginary-parser'
702
+ import { insertUser } from 'imaginary-database'
703
+ import { sendNotification } from 'imaginary-service'
704
+
705
+ // ^ assume parseUserInput, insertUser and sendNotification have the following signatures:
706
+ // parseUserInput(input: RequestData): Result<User, ParseError>
707
+ // insertUser(user: User): ResultAsync<void, InsertError>
708
+ // sendNotification(user: User): ResultAsync<void, NotificationError>
709
+ // Note insertUser returns void upon success but sendNotification takes User type.
710
+
711
+ const resAsync = parseUserInput(userInput)
712
+ .asyncAndThrough(insertUser)
713
+ .andThen(sendNotification)
714
+
715
+ resAsync.then((res: Result<void, ParseError | InsertError | NotificationError>) => {e
716
+ if(res.isErr()){
717
+ console.log("Oops, at least one step failed", res.error)
718
+ }
719
+ else{
720
+ console.log("User has been parsed, inserted and notified successfully.")
721
+ }
722
+ }))
723
+ ```
724
+
725
+ [⬆️ Back to top](#toc)
726
+
727
+ ---
728
+ #### `Result.fromThrowable` (static class method)
729
+
730
+ > Although Result is not an actual JS class, the way that `fromThrowable` has been implemented requires that you call `fromThrowable` as though it were a static method on `Result`. See examples below.
731
+
732
+ The JavaScript community has agreed on the convention of throwing exceptions.
733
+ As such, when interfacing with third party libraries it's imperative that you
734
+ wrap third-party code in try / catch blocks.
735
+
736
+ This function will create a new function that returns an `Err` when the original
737
+ function throws.
738
+
739
+ It is not possible to know the types of the errors thrown in the original
740
+ function, therefore it is recommended to use the second argument `errorFn` to
741
+ map what is thrown to a known type.
742
+
743
+ **Example**:
744
+
745
+ ```typescript
746
+ import { Result } from 'neverthrow'
747
+
748
+ type ParseError = { message: string }
749
+ const toParseError = (): ParseError => ({ message: "Parse Error" })
750
+
751
+ const safeJsonParse = Result.fromThrowable(JSON.parse, toParseError)
752
+
753
+ // the function can now be used safely, if the function throws, the result will be an Err
754
+ const res = safeJsonParse("{");
755
+ ```
756
+
757
+ [⬆️ Back to top](#toc)
758
+
759
+ ---
760
+
761
+ #### `Result.combine` (static class method)
762
+
763
+ > Although Result is not an actual JS class, the way that `combine` has been implemented requires that you call `combine` as though it were a static method on `Result`. See examples below.
764
+
765
+ Combine lists of `Result`s.
766
+
767
+ If you're familiar with `Promise.all`, the combine function works conceptually the same.
768
+
769
+ **`combine` works on both heterogeneous and homogeneous lists**. This means that you can have lists that contain different kinds of `Result`s and still be able to combine them. Note that you cannot combine lists that contain both `Result`s **and** `ResultAsync`s.
770
+
771
+ The combine function takes a list of results and returns a single result. If all the results in the list are `Ok`, then the return value will be a `Ok` containing a list of all the individual `Ok` values.
772
+
773
+ If just one of the results in the list is an `Err` then the combine function returns that Err value (it short circuits and returns the first Err that it finds).
774
+
775
+ Formally speaking:
776
+
777
+ ```typescript
778
+ // homogeneous lists
779
+ function combine<T, E>(resultList: Result<T, E>[]): Result<T[], E>
780
+
781
+ // heterogeneous lists
782
+ function combine<T1, T2, E1, E2>(resultList: [ Result<T1, E1>, Result<T2, E2> ]): Result<[ T1, T2 ], E1 | E2>
783
+ function combine<T1, T2, T3, E1, E2, E3> => Result<[ T1, T2, T3 ], E1 | E2 | E3>
784
+ function combine<T1, T2, T3, T4, E1, E2, E3, E4> => Result<[ T1, T2, T3, T4 ], E1 | E2 | E3 | E4>
785
+ // ... etc etc ad infinitum
786
+
787
+ ```
788
+
789
+ Example:
790
+ ```typescript
791
+ const resultList: Result<number, never>[] =
792
+ [ok(1), ok(2)]
793
+
794
+ const combinedList: Result<number[], unknown> =
795
+ Result.combine(resultList)
796
+ ```
797
+
798
+ Example with tuples:
799
+ ```typescript
800
+ /** @example tuple(1, 2, 3) === [1, 2, 3] // with type [number, number, number] */
801
+ const tuple = <T extends any[]>(...args: T): T => args
802
+
803
+ const resultTuple: [Result<string, never>, Result<string, never>] =
804
+ tuple(ok('a'), ok('b'))
805
+
806
+ const combinedTuple: Result<[string, string], unknown> =
807
+ Result.combine(resultTuple)
808
+ ```
809
+
810
+ [⬆️ Back to top](#toc)
811
+
812
+ ---
813
+
814
+ #### `Result.combineWithAllErrors` (static class method)
815
+
816
+ > Although Result is not an actual JS class, the way that `combineWithAllErrors` has been implemented requires that you call `combineWithAllErrors` as though it were a static method on `Result`. See examples below.
817
+
818
+ Like `combine` but without short-circuiting. Instead of just the first error value, you get a list of all error values of the input result list.
819
+
820
+ If only some results fail, the new combined error list will only contain the error value of the failed results, meaning that there is no guarantee of the length of the new error list.
821
+
822
+ Function signature:
823
+
824
+ ```typescript
825
+ // homogeneous lists
826
+ function combineWithAllErrors<T, E>(resultList: Result<T, E>[]): Result<T[], E[]>
827
+
828
+ // heterogeneous lists
829
+ function combineWithAllErrors<T1, T2, E1, E2>(resultList: [ Result<T1, E1>, Result<T2, E2> ]): Result<[ T1, T2 ], (E1 | E2)[]>
830
+ function combineWithAllErrors<T1, T2, T3, E1, E2, E3> => Result<[ T1, T2, T3 ], (E1 | E2 | E3)[]>
831
+ function combineWithAllErrors<T1, T2, T3, T4, E1, E2, E3, E4> => Result<[ T1, T2, T3, T4 ], (E1 | E2 | E3 | E4)[]>
832
+ // ... etc etc ad infinitum
833
+ ```
834
+
835
+ Example usage:
836
+
837
+ ```typescript
838
+ const resultList: Result<number, string>[] = [
839
+ ok(123),
840
+ err('boooom!'),
841
+ ok(456),
842
+ err('ahhhhh!'),
843
+ ]
844
+
845
+ const result = Result.combineWithAllErrors(resultList)
846
+
847
+ // result is Err(['boooom!', 'ahhhhh!'])
848
+ ```
849
+
850
+ [⬆️ Back to top](#toc)
851
+
852
+ #### `Result.safeUnwrap()`
853
+
854
+ **Deprecated**. You don't need to use this method anymore.
855
+
856
+ Allows for unwrapping a `Result` or returning an `Err` implicitly, thereby reducing boilerplate.
857
+
858
+
859
+ [⬆️ Back to top](#toc)
860
+
861
+ ---
862
+
863
+ ### Asynchronous API (`ResultAsync`)
864
+
865
+ #### `okAsync`
866
+
867
+ Constructs an `Ok` variant of `ResultAsync`
868
+
869
+ **Signature:**
870
+
871
+ ```typescript
872
+ okAsync<T, E>(value: T): ResultAsync<T, E>
873
+ ```
874
+
875
+ **Example:**
876
+
877
+ ```typescript
878
+ import { okAsync } from 'neverthrow'
879
+
880
+ const myResultAsync = okAsync({ myData: 'test' }) // instance of `ResultAsync`
881
+
882
+ const myResult = await myResultAsync // instance of `Ok`
883
+
884
+ myResult.isOk() // true
885
+ myResult.isErr() // false
886
+ ```
887
+
888
+ [⬆️ Back to top](#toc)
889
+
890
+ ---
891
+
892
+ #### `errAsync`
893
+
894
+ Constructs an `Err` variant of `ResultAsync`
895
+
896
+ **Signature:**
897
+
898
+ ```typescript
899
+ errAsync<T, E>(error: E): ResultAsync<T, E>
900
+ ```
901
+
902
+ **Example:**
903
+
904
+ ```typescript
905
+ import { errAsync } from 'neverthrow'
906
+
907
+ const myResultAsync = errAsync('Oh nooo') // instance of `ResultAsync`
908
+
909
+ const myResult = await myResultAsync // instance of `Err`
910
+
911
+ myResult.isOk() // false
912
+ myResult.isErr() // true
913
+ ```
914
+
915
+ [⬆️ Back to top](#toc)
916
+
917
+ ---
918
+
919
+ #### `ResultAsync.fromThrowable` (static class method)
920
+
921
+ Similar to [Result.fromThrowable](#resultfromthrowable-static-class-method), but for functions that return a `Promise`.
922
+
923
+ **Example**:
924
+
925
+ ```typescript
926
+ import { ResultAsync } from 'neverthrow'
927
+ import { insertIntoDb } from 'imaginary-database'
928
+ // insertIntoDb(user: User): Promise<User>
929
+
930
+ const insertUser = ResultAsync.fromThrowable(insertIntoDb, () => new Error('Database error'))
931
+ // `res` has a type of (user: User) => ResultAsync<User, Error>
932
+ ```
933
+
934
+ Note that this can be safer than using [ResultAsync.fromPromise](#resultasyncfrompromise-static-class-method) with
935
+ the result of a function call, because not all functions that return a `Promise` are `async`, and thus they can throw
936
+ errors synchronously rather than returning a rejected `Promise`. For example:
937
+
938
+ ```typescript
939
+ // NOT SAFE !!
940
+ import { ResultAsync } from 'neverthrow'
941
+ import { db } from 'imaginary-database'
942
+ // db.insert<T>(table: string, value: T): Promise<T>
943
+
944
+ const insertUser = (user: User): Promise<User> => {
945
+ if (!user.id) {
946
+ // this throws synchronously!
947
+ throw new TypeError('missing user id')
948
+ }
949
+ return db.insert('users', user)
950
+ }
951
+
952
+ // this will throw, NOT return a `ResultAsync`
953
+ const res = ResultAsync.fromPromise(insertIntoDb(myUser), () => new Error('Database error'))
954
+ ```
955
+
956
+ [⬆️ Back to top](#toc)
957
+
958
+ ---
959
+
960
+ #### `ResultAsync.fromPromise` (static class method)
961
+
962
+ Transforms a `PromiseLike<T>` (that may throw) into a `ResultAsync<T, E>`.
963
+
964
+ The second argument handles the rejection case of the promise and maps the error from `unknown` into some type `E`.
965
+
966
+
967
+ **Signature:**
968
+
969
+ ```typescript
970
+ // fromPromise is a static class method
971
+ // also available as a standalone function
972
+ // import { fromPromise } from 'neverthrow'
973
+ ResultAsync.fromPromise<T, E>(
974
+ promise: PromiseLike<T>,
975
+ errorHandler: (unknownError: unknown) => E)
976
+ ): ResultAsync<T, E> { ... }
977
+ ```
978
+
979
+ If you are working with `PromiseLike` objects that you **know for a fact** will not throw, then use `fromSafePromise` in order to avoid having to pass a redundant `errorHandler` argument.
980
+
981
+ **Example**:
982
+
983
+ ```typescript
984
+ import { ResultAsync } from 'neverthrow'
985
+ import { insertIntoDb } from 'imaginary-database'
986
+ // insertIntoDb(user: User): Promise<User>
987
+
988
+ const res = ResultAsync.fromPromise(insertIntoDb(myUser), () => new Error('Database error'))
989
+ // `res` has a type of ResultAsync<User, Error>
990
+ ```
991
+
992
+ [⬆️ Back to top](#toc)
993
+
994
+ ---
995
+
996
+ #### `ResultAsync.fromSafePromise` (static class method)
997
+
998
+ Same as `ResultAsync.fromPromise` except that it does not handle the rejection of the promise. **Ensure you know what you're doing, otherwise a thrown exception within this promise will cause ResultAsync to reject, instead of resolve to a Result.**
999
+
1000
+ **Signature:**
1001
+
1002
+ ```typescript
1003
+ // fromPromise is a static class method
1004
+ // also available as a standalone function
1005
+ // import { fromPromise } from 'neverthrow'
1006
+ ResultAsync.fromSafePromise<T, E>(
1007
+ promise: PromiseLike<T>
1008
+ ): ResultAsync<T, E> { ... }
1009
+ ```
1010
+
1011
+ **Example**:
1012
+
1013
+ ```typescript
1014
+ import { RouteError } from 'routes/error'
1015
+
1016
+ // simulate slow routes in an http server that works in a Result / ResultAsync context
1017
+ // Adopted from https://github.com/parlez-vous/server/blob/2496bacf55a2acbebc30631b5562f34272794d76/src/routes/common/signup.ts
1018
+ export const slowDown = <T>(ms: number) => (value: T) =>
1019
+ ResultAsync.fromSafePromise<T, RouteError>(
1020
+ new Promise((resolve) => {
1021
+ setTimeout(() => {
1022
+ resolve(value)
1023
+ }, ms)
1024
+ })
1025
+ )
1026
+
1027
+ export const signupHandler = route<User>((req, sessionManager) =>
1028
+ decode(userSignupDecoder, req.body, 'Invalid request body').map((parsed) => {
1029
+ return createUser(parsed)
1030
+ .andThen(slowDown(3000)) // slowdown by 3 seconds
1031
+ .andThen(sessionManager.createSession)
1032
+ .map(({ sessionToken, admin }) => AppData.init(admin, sessionToken))
1033
+ })
1034
+ )
1035
+ ```
1036
+
1037
+ [⬆️ Back to top](#toc)
1038
+
1039
+ ---
1040
+
1041
+ #### `ResultAsync.map` (method)
1042
+
1043
+ Maps a `ResultAsync<T, E>` to `ResultAsync<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value untouched.
1044
+
1045
+ The applied function can be synchronous or asynchronous (returning a `Promise<U>`) with no impact to the return type.
1046
+
1047
+ This function can be used to compose the results of two functions.
1048
+
1049
+ **Signature:**
1050
+
1051
+ ```typescript
1052
+ class ResultAsync<T, E> {
1053
+ map<U>(
1054
+ callback: (value: T) => U | Promise<U>
1055
+ ): ResultAsync<U, E> { ... }
1056
+ }
1057
+ ```
1058
+
1059
+ **Example**:
1060
+
1061
+ ```typescript
1062
+ import { findUsersIn } from 'imaginary-database'
1063
+ // ^ assume findUsersIn has the following signature:
1064
+ // findUsersIn(country: string): ResultAsync<Array<User>, Error>
1065
+
1066
+ const usersInCanada = findUsersIn("Canada")
1067
+
1068
+ // Let's assume we only need their names
1069
+ const namesInCanada = usersInCanada.map((users: Array<User>) => users.map(user => user.name))
1070
+ // namesInCanada is of type ResultAsync<Array<string>, Error>
1071
+
1072
+ // We can extract the Result using .then() or await
1073
+ namesInCanada.then((namesResult: Result<Array<string>, Error>) => {
1074
+ if(namesResult.isErr()){
1075
+ console.log("Couldn't get the users from the database", namesResult.error)
1076
+ }
1077
+ else{
1078
+ console.log("Users in Canada are named: " + namesResult.value.join(','))
1079
+ }
1080
+ })
1081
+ ```
1082
+
1083
+ [⬆️ Back to top](#toc)
1084
+
1085
+ ---
1086
+
1087
+ #### `ResultAsync.mapErr` (method)
1088
+
1089
+ Maps a `ResultAsync<T, E>` to `ResultAsync<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value untouched.
1090
+
1091
+ The applied function can be synchronous or asynchronous (returning a `Promise<F>`) with no impact to the return type.
1092
+
1093
+ This function can be used to pass through a successful result while handling an error.
1094
+
1095
+ **Signature:**
1096
+
1097
+ ```typescript
1098
+ class ResultAsync<T, E> {
1099
+ mapErr<F>(
1100
+ callback: (error: E) => F | Promise<F>
1101
+ ): ResultAsync<T, F> { ... }
1102
+ }
1103
+ ```
1104
+
1105
+ **Example**:
1106
+
1107
+ ```typescript
1108
+ import { findUsersIn } from 'imaginary-database'
1109
+ // ^ assume findUsersIn has the following signature:
1110
+ // findUsersIn(country: string): ResultAsync<Array<User>, Error>
1111
+
1112
+ // Let's say we need to low-level errors from findUsersIn to be more readable
1113
+ const usersInCanada = findUsersIn("Canada").mapErr((error: Error) => {
1114
+ // The only error we want to pass to the user is "Unknown country"
1115
+ if(error.message === "Unknown country"){
1116
+ return error.message
1117
+ }
1118
+ // All other errors will be labelled as a system error
1119
+ return "System error, please contact an administrator."
1120
+ })
1121
+
1122
+ // usersInCanada is of type ResultAsync<Array<User>, string>
1123
+
1124
+ usersInCanada.then((usersResult: Result<Array<User>, string>) => {
1125
+ if(usersResult.isErr()){
1126
+ res.status(400).json({
1127
+ error: usersResult.error
1128
+ })
1129
+ }
1130
+ else{
1131
+ res.status(200).json({
1132
+ users: usersResult.value
1133
+ })
1134
+ }
1135
+ })
1136
+ ```
1137
+
1138
+ [⬆️ Back to top](#toc)
1139
+
1140
+ ---
1141
+
1142
+ #### `ResultAsync.unwrapOr` (method)
1143
+
1144
+ Unwrap the `Ok` value, or return the default if there is an `Err`.
1145
+ Works just like `Result.unwrapOr` but returns a `Promise<T>` instead of `T`.
1146
+
1147
+ **Signature:**
1148
+
1149
+ ```typescript
1150
+ class ResultAsync<T, E> {
1151
+ unwrapOr<T>(value: T): Promise<T> { ... }
1152
+ }
1153
+ ```
1154
+
1155
+ **Example**:
1156
+
1157
+ ```typescript
1158
+ const unwrapped: number = await errAsync(0).unwrapOr(10)
1159
+ // unwrapped = 10
1160
+ ```
1161
+
1162
+ [⬆️ Back to top](#toc)
1163
+
1164
+ ---
1165
+
1166
+ #### `ResultAsync.andThen` (method)
1167
+
1168
+ Same idea as `map` above. Except the applied function must return a `Result` or `ResultAsync`.
1169
+
1170
+ `ResultAsync.andThen` always returns a `ResultAsync` no matter the return type of the applied function.
1171
+
1172
+ This is useful for when you need to do a subsequent computation using the inner `T` value, but that computation might fail.
1173
+
1174
+ `andThen` is really useful as a tool to flatten a `ResultAsync<ResultAsync<A, E2>, E1>` into a `ResultAsync<A, E2>` (see example below).
1175
+
1176
+ **Signature:**
1177
+
1178
+ ```typescript
1179
+ // Note that the latest version (v4.1.0-beta) lets you return distinct errors as well.
1180
+ // If the error types (E and F) are the same (like `string | string`)
1181
+ // then they will be merged into one type (`string`)
1182
+
1183
+ class ResultAsync<T, E> {
1184
+ andThen<U, F>(
1185
+ callback: (value: T) => Result<U, F> | ResultAsync<U, F>
1186
+ ): ResultAsync<U, E | F> { ... }
1187
+ }
1188
+ ```
1189
+
1190
+ **Example**
1191
+
1192
+ ```typescript
1193
+
1194
+ import { validateUser } from 'imaginary-validator'
1195
+ import { insertUser } from 'imaginary-database'
1196
+ import { sendNotification } from 'imaginary-service'
1197
+
1198
+ // ^ assume validateUser, insertUser and sendNotification have the following signatures:
1199
+ // validateUser(user: User): Result<User, Error>
1200
+ // insertUser(user): ResultAsync<User, Error>
1201
+ // sendNotification(user): ResultAsync<void, Error>
1202
+
1203
+ const resAsync = validateUser(user)
1204
+ .andThen(insertUser)
1205
+ .andThen(sendNotification)
1206
+
1207
+ // resAsync is a ResultAsync<void, Error>
1208
+
1209
+ resAsync.then((res: Result<void, Error>) => {
1210
+ if(res.isErr()){
1211
+ console.log("Oops, at least one step failed", res.error)
1212
+ }
1213
+ else{
1214
+ console.log("User has been validated, inserted and notified successfully.")
1215
+ }
1216
+ })
1217
+ ```
1218
+
1219
+ [⬆️ Back to top](#toc)
1220
+
1221
+ ---
1222
+
1223
+ #### `ResultAsync.orElse` (method)
1224
+
1225
+ Takes an `Err` value and maps it to a `ResultAsync<T, SomeNewType>`. This is useful for error recovery.
1226
+
1227
+ **Signature:**
1228
+
1229
+ ```typescript
1230
+ class ResultAsync<T, E> {
1231
+ orElse<U, A>(
1232
+ callback: (error: E) => Result<U, A> | ResultAsync<U, A>
1233
+ ): ResultAsync<U | T, A> { ... }
1234
+ }
1235
+ ```
1236
+
1237
+ [⬆️ Back to top](#toc)
1238
+
1239
+ ---
1240
+
1241
+ #### `ResultAsync.match` (method)
1242
+
1243
+ Given 2 functions (one for the `Ok` variant and one for the `Err` variant) execute the function that matches the `ResultAsync` variant.
1244
+
1245
+ The difference with `Result.match` is that it always returns a `Promise` because of the asynchronous nature of the `ResultAsync`.
1246
+
1247
+ **Signature:**
1248
+
1249
+ ```typescript
1250
+ class ResultAsync<T, E> {
1251
+ match<A, B = A>(
1252
+ okCallback: (value: T) => A,
1253
+ errorCallback: (error: E) => B
1254
+ ): Promise<A | B> => { ... }
1255
+ }
1256
+ ```
1257
+
1258
+ **Example:**
1259
+
1260
+ ```typescript
1261
+
1262
+ import { validateUser } from 'imaginary-validator'
1263
+ import { insertUser } from 'imaginary-database'
1264
+
1265
+ // ^ assume validateUser and insertUser have the following signatures:
1266
+ // validateUser(user: User): Result<User, Error>
1267
+ // insertUser(user): ResultAsync<User, Error>
1268
+
1269
+ // Handle both cases at the end of the chain using match
1270
+ const resultMessage = await validateUser(user)
1271
+ .andThen(insertUser)
1272
+ .match(
1273
+ (user: User) => `User ${user.name} has been successfully created`,
1274
+ (error: Error) => `User could not be created because ${error.message}`
1275
+ )
1276
+
1277
+ // resultMessage is a string
1278
+ ```
1279
+
1280
+ [⬆️ Back to top](#toc)
1281
+
1282
+ ---
1283
+ #### `ResultAsync.andTee` (method)
1284
+
1285
+ Takes a `ResultAsync<T, E>` and lets the original `ResultAsync<T, E>` pass through regardless
1286
+ the result of the passed-in function.
1287
+ This is a handy way to handle side effects whose failure or success should not affect your main logics such as logging.
1288
+
1289
+ **Signature:**
1290
+
1291
+ ```typescript
1292
+ class ResultAsync<T, E> {
1293
+ andTee(
1294
+ callback: (value: T) => unknown
1295
+ ): ResultAsync<T, E> => { ... }
1296
+ }
1297
+ ```
1298
+
1299
+ **Example:**
1300
+
1301
+ ```typescript
1302
+ import { insertUser } from 'imaginary-database'
1303
+ import { logUser } from 'imaginary-logger'
1304
+ import { sendNotification } from 'imaginary-service'
1305
+
1306
+ // ^ assume insertUser, logUser and sendNotification have the following signatures:
1307
+ // insertUser(user: User): ResultAsync<User, InsertError>
1308
+ // logUser(user: User): Result<void, LogError>
1309
+ // sendNotification(user: User): ResultAsync<void, NotificationError>
1310
+ // Note logUser returns void on success but sendNotification takes User type.
1311
+
1312
+ const resAsync = insertUser(user)
1313
+ .andTee(logUser)
1314
+ .andThen(sendNotification)
1315
+
1316
+ // Note there is no LogError in the types below
1317
+ resAsync.then((res: Result<void, InsertError | NotificationError>) => {e
1318
+ if(res.isErr()){
1319
+ console.log("Oops, at least one step failed", res.error)
1320
+ }
1321
+ else{
1322
+ console.log("User has been inserted and notified successfully.")
1323
+ }
1324
+ }))
1325
+ ```
1326
+
1327
+ [⬆️ Back to top](#toc)
1328
+
1329
+ ---
1330
+ #### `ResultAsync.orTee` (method)
1331
+
1332
+ Like `andTee` for the error track. Takes a `ResultAsync<T, E>` and lets the original `Err` value pass through regardless
1333
+ the result of the passed-in function.
1334
+ This is a handy way to handle side effects whose failure or success should not affect your main logics such as logging.
1335
+
1336
+ **Signature:**
1337
+
1338
+ ```typescript
1339
+ class ResultAsync<T, E> {
1340
+ orTee(
1341
+ callback: (value: E) => unknown
1342
+ ): ResultAsync<T, E> => { ... }
1343
+ }
1344
+ ```
1345
+
1346
+ **Example:**
1347
+
1348
+ ```typescript
1349
+ import { insertUser } from 'imaginary-database'
1350
+ import { logInsertError } from 'imaginary-logger'
1351
+ import { sendNotification } from 'imaginary-service'
1352
+
1353
+ // ^ assume insertUser, logInsertError and sendNotification have the following signatures:
1354
+ // insertUser(user: User): ResultAsync<User, InsertError>
1355
+ // logInsertError(insertError: InsertError): Result<void, LogError>
1356
+ // sendNotification(user: User): ResultAsync<void, NotificationError>
1357
+ // Note logInsertError returns void on success but sendNotification takes User type.
1358
+
1359
+ const resAsync = insertUser(user)
1360
+ .orTee(logUser)
1361
+ .andThen(sendNotification)
1362
+
1363
+ // Note there is no LogError in the types below
1364
+ resAsync.then((res: Result<void, InsertError | NotificationError>) => {e
1365
+ if(res.isErr()){
1366
+ console.log("Oops, at least one step failed", res.error)
1367
+ }
1368
+ else{
1369
+ console.log("User has been inserted and notified successfully.")
1370
+ }
1371
+ }))
1372
+ ```
1373
+
1374
+ [⬆️ Back to top](#toc)
1375
+
1376
+ ---
1377
+ #### `ResultAsync.andThrough` (method)
1378
+
1379
+
1380
+ Similar to `andTee` except for:
1381
+
1382
+ - when there is an error from the passed-in function, that error will be passed along.
1383
+
1384
+ **Signature:**
1385
+
1386
+ ```typescript
1387
+ class ResultAsync<T, E> {
1388
+ andThrough<F>(
1389
+ callback: (value: T) => Result<unknown, F> | ResultAsync<unknown, F>,
1390
+ ): ResultAsync<T, E | F> => { ... }
1391
+ }
1392
+ ```
1393
+
1394
+ **Example:**
1395
+
1396
+ ```typescript
1397
+
1398
+ import { buildUser } from 'imaginary-builder'
1399
+ import { insertUser } from 'imaginary-database'
1400
+ import { sendNotification } from 'imaginary-service'
1401
+
1402
+ // ^ assume buildUser, insertUser and sendNotification have the following signatures:
1403
+ // buildUser(userRaw: UserRaw): ResultAsync<User, BuildError>
1404
+ // insertUser(user: User): ResultAsync<void, InsertError>
1405
+ // sendNotification(user: User): ResultAsync<void, NotificationError>
1406
+ // Note insertUser returns void upon success but sendNotification takes User type.
1407
+
1408
+ const resAsync = buildUser(userRaw)
1409
+ .andThrough(insertUser)
1410
+ .andThen(sendNotification)
1411
+
1412
+ resAsync.then((res: Result<void, BuildError | InsertError | NotificationError>) => {e
1413
+ if(res.isErr()){
1414
+ console.log("Oops, at least one step failed", res.error)
1415
+ }
1416
+ else{
1417
+ console.log("User data has been built, inserted and notified successfully.")
1418
+ }
1419
+ }))
1420
+ ```
1421
+
1422
+ [⬆️ Back to top](#toc)
1423
+
1424
+ ---
1425
+ #### `ResultAsync.combine` (static class method)
1426
+
1427
+ Combine lists of `ResultAsync`s.
1428
+
1429
+ If you're familiar with `Promise.all`, the combine function works conceptually the same.
1430
+
1431
+ **`combine` works on both heterogeneous and homogeneous lists**. This means that you can have lists that contain different kinds of `ResultAsync`s and still be able to combine them. Note that you cannot combine lists that contain both `Result`s **and** `ResultAsync`s.
1432
+
1433
+ The combine function takes a list of results and returns a single result. If all the results in the list are `Ok`, then the return value will be a `Ok` containing a list of all the individual `Ok` values.
1434
+
1435
+ If just one of the results in the list is an `Err` then the combine function returns that Err value (it short circuits and returns the first Err that it finds).
1436
+
1437
+ Formally speaking:
1438
+
1439
+ ```typescript
1440
+ // homogeneous lists
1441
+ function combine<T, E>(resultList: ResultAsync<T, E>[]): ResultAsync<T[], E>
1442
+
1443
+ // heterogeneous lists
1444
+ function combine<T1, T2, E1, E2>(resultList: [ ResultAsync<T1, E1>, ResultAsync<T2, E2> ]): ResultAsync<[ T1, T2 ], E1 | E2>
1445
+ function combine<T1, T2, T3, E1, E2, E3> => ResultAsync<[ T1, T2, T3 ], E1 | E2 | E3>
1446
+ function combine<T1, T2, T3, T4, E1, E2, E3, E4> => ResultAsync<[ T1, T2, T3, T4 ], E1 | E2 | E3 | E4>
1447
+ // ... etc etc ad infinitum
1448
+
1449
+ ```
1450
+
1451
+ Example:
1452
+ ```typescript
1453
+ const resultList: ResultAsync<number, never>[] =
1454
+ [okAsync(1), okAsync(2)]
1455
+
1456
+ const combinedList: ResultAsync<number[], unknown> =
1457
+ ResultAsync.combine(resultList)
1458
+ ```
1459
+
1460
+ Example with tuples:
1461
+ ```typescript
1462
+ /** @example tuple(1, 2, 3) === [1, 2, 3] // with type [number, number, number] */
1463
+ const tuple = <T extends any[]>(...args: T): T => args
1464
+
1465
+ const resultTuple: [ResultAsync<string, never>, ResultAsync<string, never>] =
1466
+ tuple(okAsync('a'), okAsync('b'))
1467
+
1468
+ const combinedTuple: ResultAsync<[string, string], unknown> =
1469
+ ResultAsync.combine(resultTuple)
1470
+ ```
1471
+ [⬆️ Back to top](#toc)
1472
+
1473
+ ---
1474
+
1475
+ #### `ResultAsync.combineWithAllErrors` (static class method)
1476
+
1477
+ Like `combine` but without short-circuiting. Instead of just the first error value, you get a list of all error values of the input result list.
1478
+
1479
+ If only some results fail, the new combined error list will only contain the error value of the failed results, meaning that there is no guarantee of the length of the new error list.
1480
+
1481
+ Function signature:
1482
+
1483
+ ```typescript
1484
+ // homogeneous lists
1485
+ function combineWithAllErrors<T, E>(resultList: ResultAsync<T, E>[]): ResultAsync<T[], E[]>
1486
+
1487
+ // heterogeneous lists
1488
+ function combineWithAllErrors<T1, T2, E1, E2>(resultList: [ ResultAsync<T1, E1>, ResultAsync<T2, E2> ]): ResultAsync<[ T1, T2 ], (E1 | E2)[]>
1489
+ function combineWithAllErrors<T1, T2, T3, E1, E2, E3> => ResultAsync<[ T1, T2, T3 ], (E1 | E2 | E3)[]>
1490
+ function combineWithAllErrors<T1, T2, T3, T4, E1, E2, E3, E4> => ResultAsync<[ T1, T2, T3, T4 ], (E1 | E2 | E3 | E4)[]>
1491
+ // ... etc etc ad infinitum
1492
+ ```
1493
+
1494
+ Example usage:
1495
+
1496
+ ```typescript
1497
+ const resultList: ResultAsync<number, string>[] = [
1498
+ okAsync(123),
1499
+ errAsync('boooom!'),
1500
+ okAsync(456),
1501
+ errAsync('ahhhhh!'),
1502
+ ]
1503
+
1504
+ const result = ResultAsync.combineWithAllErrors(resultList)
1505
+
1506
+ // result is Err(['boooom!', 'ahhhhh!'])
1507
+ ```
1508
+
1509
+ #### `ResultAsync.safeUnwrap()`
1510
+
1511
+ **Deprecated**. You don't need to use this method anymore.
1512
+
1513
+ Allows for unwrapping a `Result` or returning an `Err` implicitly, thereby reducing boilerplate.
1514
+
1515
+ [⬆️ Back to top](#toc)
1516
+
1517
+ ---
1518
+
1519
+ ### Utilities
1520
+
1521
+ #### `fromThrowable`
1522
+
1523
+ Top level export of `Result.fromThrowable`.
1524
+ Please find documentation at [Result.fromThrowable](#resultfromthrowable-static-class-method)
1525
+
1526
+ [⬆️ Back to top](#toc)
1527
+
1528
+ #### `fromAsyncThrowable`
1529
+
1530
+ Top level export of `ResultAsync.fromThrowable`.
1531
+ Please find documentation at [ResultAsync.fromThrowable](#resultasyncfromthrowable-static-class-method)
1532
+
1533
+ [⬆️ Back to top](#toc)
1534
+
1535
+ #### `fromPromise`
1536
+
1537
+ Top level export of `ResultAsync.fromPromise`.
1538
+ Please find documentation at [ResultAsync.fromPromise](#resultasyncfrompromise-static-class-method)
1539
+
1540
+ [⬆️ Back to top](#toc)
1541
+
1542
+ #### `fromSafePromise`
1543
+
1544
+ Top level export of `ResultAsync.fromSafePromise`.
1545
+ Please find documentation at [ResultAsync.fromSafePromise](#resultasyncfromsafepromise-static-class-method)
1546
+
1547
+ [⬆️ Back to top](#toc)
1548
+
1549
+ #### `safeTry`
1550
+
1551
+ Used to implicitly return errors and reduce boilerplate.
1552
+
1553
+ Let's say we are writing a function that returns a `Result`, and in that function we call some functions which also return `Result`s and we check those results to see whether we should keep going or abort. Usually, we will write like the following.
1554
+ ```typescript
1555
+ declare function mayFail1(): Result<number, string>;
1556
+ declare function mayFail2(): Result<number, string>;
1557
+
1558
+ function myFunc(): Result<number, string> {
1559
+ // We have to define a constant to hold the result to check and unwrap its value.
1560
+ const result1 = mayFail1();
1561
+ if (result1.isErr()) {
1562
+ return err(`aborted by an error from 1st function, ${result1.error}`);
1563
+ }
1564
+ const value1 = result1.value
1565
+
1566
+ // Again, we need to define a constant and then check and unwrap.
1567
+ const result2 = mayFail2();
1568
+ if (result2.isErr()) {
1569
+ return err(`aborted by an error from 2nd function, ${result2.error}`);
1570
+ }
1571
+ const value2 = result2.value
1572
+
1573
+ // And finally we return what we want to calculate
1574
+ return ok(value1 + value2);
1575
+ }
1576
+ ```
1577
+ Basically, we need to define a constant for each result to check whether it's a `Ok` and read its `.value` or `.error`.
1578
+
1579
+ With safeTry, we can state 'Return here if its an `Err`, otherwise unwrap it here and keep going.' in just one expression.
1580
+ ```typescript
1581
+ declare function mayFail1(): Result<number, string>;
1582
+ declare function mayFail2(): Result<number, string>;
1583
+
1584
+ function myFunc(): Result<number, string> {
1585
+ return safeTry<number, string>(function*() {
1586
+ return ok(
1587
+ // If the result of mayFail1().mapErr() is an `Err`, the evaluation is
1588
+ // aborted here and the enclosing `safeTry` block is evaluated to that `Err`.
1589
+ // Otherwise, this `(yield* ...)` is evaluated to its `.value`.
1590
+ (yield* mayFail1()
1591
+ .mapErr(e => `aborted by an error from 1st function, ${e}`))
1592
+ +
1593
+ // The same as above.
1594
+ (yield* mayFail2()
1595
+ .mapErr(e => `aborted by an error from 2nd function, ${e}`))
1596
+ )
1597
+ })
1598
+ }
1599
+ ```
1600
+
1601
+ To use `safeTry`, the points are as follows.
1602
+ * Wrap the entire block in a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*)
1603
+ * In that block, you can use `yield* <RESULT>` to state 'Return `<RESULT>` if it's an `Err`, otherwise evaluate to its `.value`'
1604
+ * Pass the generator function to `safeTry`
1605
+
1606
+ You can also use [async generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) to pass an async block to `safeTry`.
1607
+ ```typescript
1608
+ // You can use either Promise<Result> or ResultAsync.
1609
+ declare function mayFail1(): Promise<Result<number, string>>;
1610
+ declare function mayFail2(): ResultAsync<number, string>;
1611
+
1612
+ function myFunc(): Promise<Result<number, string>> {
1613
+ return safeTry<number, string>(async function*() {
1614
+ return ok(
1615
+ // You have to await if the expression is Promise<Result>
1616
+ (yield* (await mayFail1())
1617
+ .mapErr(e => `aborted by an error from 1st function, ${e}`))
1618
+ +
1619
+ // You can call `safeUnwrap` directly if its ResultAsync
1620
+ (yield* mayFail2()
1621
+ .mapErr(e => `aborted by an error from 2nd function, ${e}`))
1622
+ )
1623
+ })
1624
+ }
1625
+ ```
1626
+
1627
+ For more information, see https://github.com/supermacro/neverthrow/pull/448 and https://github.com/supermacro/neverthrow/issues/444
1628
+
1629
+ [⬆️ Back to top](#toc)
1630
+
1631
+ ---
1632
+
1633
+ ### Testing
1634
+
1635
+ `Result` instances have two unsafe methods, aptly called `_unsafeUnwrap` and `_unsafeUnwrapErr` which **should only be used in a test environment**.
1636
+
1637
+ `_unsafeUnwrap` takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
1638
+
1639
+ `_unsafeUnwrapErr` takes a `Result<T, E>` and returns a `E` when the result is an `Err`, otherwise it throws a custom object.
1640
+
1641
+ That way you can do something like:
1642
+
1643
+ ```typescript
1644
+ expect(myResult._unsafeUnwrap()).toBe(someExpectation)
1645
+ ```
1646
+
1647
+ However, do note that `Result` instances are comparable. So you don't necessarily need to unwrap them in order to assert expectations in your tests. So you could also do something like this:
1648
+
1649
+ ```typescript
1650
+ import { ok } from 'neverthrow'
1651
+
1652
+ // ...
1653
+
1654
+ expect(callSomeFunctionThatReturnsAResult("with", "some", "args")).toEqual(ok(someExpectation));
1655
+ ```
1656
+
1657
+ By default, the thrown value does not contain a stack trace. This is because stack trace generation [makes error messages in Jest harder to understand](https://github.com/supermacro/neverthrow/pull/215). If you want stack traces to be generated, call `_unsafeUnwrap` and / or `_unsafeUnwrapErr` with a config object:
1658
+
1659
+ ```typescript
1660
+ _unsafeUnwrapErr({
1661
+ withStackTrace: true,
1662
+ })
1663
+
1664
+ // ^ Now the error object will have a `.stack` property containing the current stack
1665
+ ```
1666
+
1667
+ ---
1668
+
1669
+ If you find this package useful, please consider [sponsoring me](https://github.com/sponsors/supermacro/) or simply [buying me a coffee](https://ko-fi.com/gdelgado)!
1670
+
1671
+ ---
1672
+
1673
+ ## A note on the Package Name
1674
+
1675
+ Although the package is called `neverthrow`, please don't take this literally. I am simply encouraging the developer to think a bit more about the ergonomics and usage of whatever software they are writing.
1676
+
1677
+ `Throw`ing and `catching` is very similar to using `goto` statements - in other words; it makes reasoning about your programs harder. Secondly, by using `throw` you make the assumption that the caller of your function is implementing `catch`. This is a known source of errors. Example: One dev `throw`s and another dev uses the function without prior knowledge that the function will throw. Thus, and edge case has been left unhandled and now you have unhappy users, bosses, cats, etc.
1678
+
1679
+ With all that said, there are definitely good use cases for throwing in your program. But much less than you might think.
1680
+
1681
+ ### License
1682
+
1683
+ The neverthrow project is available as open source under the terms of the [MIT license](https://github.com/supermacro/neverthrow/blob/master/LICENSE).