MCPcopy Create free account
hub / github.com/Effect-TS/effect / tryPromise

Function tryPromise

packages/effect/src/internal/effect.ts:1226–1255  ·  view source on GitHub ↗
(
  options: {
    readonly try: (signal: AbortSignal) => PromiseLike<A>
    readonly catch: (error: unknown) => E
  } | ((signal: AbortSignal) => PromiseLike<A>)
)

Source from the content-addressed store, hash-verified

1224
1225/** @internal */
1226export const tryPromise = <A, E = Cause.UnknownError>(
1227 options: {
1228 readonly try: (signal: AbortSignal) => PromiseLike<A>
1229 readonly catch: (error: unknown) => E
1230 } | ((signal: AbortSignal) => PromiseLike<A>)
1231): Effect.Effect<A, E> => {
1232 const f = typeof options === "function" ? options : options.try
1233 const catcher = typeof options === "function"
1234 ? ((cause: unknown) => new UnknownError(cause, "An error occurred in Effect.tryPromise"))
1235 : options.catch
1236 return callbackOptions<A, E>(function(resume, signal) {
1237 // Defer the catcher until the fiber restores its async context.
1238 const failWithCatch = (cause: unknown) =>
1239 suspend(() => {
1240 try {
1241 return fail(internalCall(() => catcher(cause)) as E)
1242 } catch (err) {
1243 return die(err)
1244 }
1245 })
1246 try {
1247 f(signal!).then(
1248 (a) => resume(succeed(a)),
1249 (e) => resume(failWithCatch(e))
1250 )
1251 } catch (err) {
1252 resume(failWithCatch(err))
1253 }
1254 }, f.length !== 0)
1255}
1256
1257/** @internal */
1258export const withFiberId = <A, E, R>(

Callers

nothing calls this directly

Calls 4

failWithCatchFunction · 0.85
succeedFunction · 0.70
fFunction · 0.50
resumeFunction · 0.50

Tested by

no test coverage detected