Effect 4.0.5 internal/effect.ts SHA256 29200eee28c991b77b9f62317831332d4330179d8ab654fec8dea4472732d0af
1099: export const tryPromise = <A, E = Cause.UnknownError>(
1100:   options: {
1101:     readonly try: (signal: AbortSignal) => PromiseLike<A>
1102:     readonly catch: (error: unknown) => E
1103:   } | ((signal: AbortSignal) => PromiseLike<A>)
1104: ): Effect.Effect<A, E> => {
1105:   const f = typeof options === "function" ? options : options.try
1106:   const catcher = typeof options === "function"
1107:     ? ((cause: unknown) => new UnknownError(cause, "An error occurred in Effect.tryPromise"))
1108:     : options.catch
1109:   return callbackOptions<A, E>(function(resume, signal) {
1110:     // Defer the catcher until the fiber restores its async context.
1111:     const failWithCatch = (cause: unknown) =>
1112:       suspend(() => {
1113:         try {
1114:           return fail(internalCall(() => catcher(cause)) as E)
1115:         } catch (err) {
1116:           return die(err)
1117:         }
1118:       })
1119:     try {
1120:       f(signal!).then(
1121:         (a) => resume(succeed(a)),
1122:         (e) => resume(failWithCatch(e))
1123:       )
1124:     } catch (err) {
1125:       resume(failWithCatch(err))
1126:     }
1127:   }, f.length !== 0)
1128: }
1129: 
1130: /** @internal */
1131: export const withFiberId = <A, E, R>(
1132:   f: (fiberId: number) => Effect.Effect<A, E, R>
1133: ): Effect.Effect<A, E, R> => withFiber((fiber) => f(fiber.id))
1134: 
1135: /** @internal */
1136: export const fiber = withFiber(succeed)
1137: 
1138: /** @internal */
1139: export const fiberId = withFiberId(succeed)
1140: 
1141: const callbackOptions: <A, E = never, R = never>(
1142:   register: (
1143:     this: Scheduler.Scheduler,
1144:     resume: (effect: Effect.Effect<A, E, R>) => void,
1145:     signal?: AbortSignal
1146:   ) => void | Effect.Effect<void, E, R>,
1147:   withSignal: boolean
1148: ) => Effect.Effect<A, E, R> = (function() {
1149:   const Proto = makePrimitiveProto({
1150:     op: "Async",
1151:     [evaluate](this: any, fiber) {
1152:       let resumed = false
1153:       let yielded: boolean | Primitive = false
1154:       const controller = this.withSignal ? new AbortController() : undefined
1155:       const onCancel = this.register.call(fiber.cache.scheduler, (effect: Effect.Effect<any, any, any>) => {
1156:         if (resumed) return
1157:         resumed = true
1158:         if (yielded) {
1159:           fiber.evaluate(effect as any)
1160:         } else {
1161:           yielded = effect as any
1162:         }
1163:       }, controller?.signal)
1164:       if (yielded !== false) return yielded
1165:       yielded = true
1166:       fiber._yielded = () => {
1167:         resumed = true
1168:       }
1169:       if (controller === undefined && onCancel === undefined) {
1170:         return Yield
1171:       }
1172:       fiber._stack.push(
1173:         asyncFinalizer(() => {
1174:           resumed = true
1175:           controller?.abort()
1176:           return onCancel ?? exitVoid
1177:         })
1178:       )
1179:       return Yield
1180:     }
1181:   })
1182:   const AsyncImpl = function(this: any, register: any, withSignal: boolean) {
1183:     this.register = register
1184:     this.withSignal = withSignal
1185:   } as unknown as PrimitiveCtor<[register: any, withSignal: boolean]>
1186:   AsyncImpl.prototype = Proto
1187:   return function(register: any, withSignal: boolean) {
1188:     return new AsyncImpl(register, withSignal)
1189:   } as any
1190: })()
1191: 
1192: const asyncFinalizer: (
1193:   onInterrupt: () => Effect.Effect<void, any, any>
1194: ) => Primitive = makePrimitive({
1195:   op: "AsyncFinalizer",
1196:   [contAll](fiber) {
1197:     if (fiber.interruptible) {
1198:       fiber.interruptible = false
1199:       fiber._stack.push(setInterruptibleTrue)
1200:     }
1201:   },
1202:   [contE](cause, _fiber) {
1203:     return hasInterrupts(cause)
1204:       ? flatMap(combineFinalizerCause(exitFailCause(cause), this[args]()), () => failCause(cause))
1205:       : failCause(cause)
1206:   }
1207: })
1208: 
1209: /** @internal */
1210: export const callback = <A, E = never, R = never>(

tryPromise chooses signal support from callback arity. Each evaluated async operation allocates one controller; its async finalizer calls abort on interruption, with finalization uninterruptible. Two interrupt requests yield one observed abort event and one target socket close per T4. Event counts alone do not prove abort() invocation counts.
