MCPcopy
hub / github.com/loopbackio/loopback-next / tryCatchFinally

Function tryCatchFinally

packages/context/src/value-promise.ts:213–247  ·  view source on GitHub ↗
(
  action: () => ValueOrPromise<T>,
  errorAction: (err: unknown) => T | never = err => {
    throw err;
  },
  finalAction: () => void = () => {},
)

Source from the content-addressed store, hash-verified

211 * @typeParam T - Type for the return value
212 */
213export function tryCatchFinally<T>(
214 action: () => ValueOrPromise<T>,
215 errorAction: (err: unknown) => T | never = err => {
216 throw err;
217 },
218 finalAction: () => void = () => {},
219): ValueOrPromise<T> {
220 let result: ValueOrPromise<T>;
221 try {
222 result = action();
223 } catch (err) {
224 result = reject(err);
225 }
226 if (isPromiseLike(result)) {
227 return result.then(resolve, reject);
228 }
229
230 return resolve(result);
231
232 function resolve(value: T) {
233 try {
234 return value;
235 } finally {
236 finalAction();
237 }
238 }
239
240 function reject(err: unknown): T | never {
241 try {
242 return errorAction(err);
243 } finally {
244 finalAction();
245 }
246 }
247}
248
249/**
250 * Resolve an iterator of source values into a result until the evaluator

Callers 2

tryWithFinallyFunction · 0.85

Calls 4

actionFunction · 0.85
isPromiseLikeFunction · 0.85
rejectFunction · 0.70
resolveFunction · 0.70

Tested by

no test coverage detected