---
lang: en
title: 'API docs: context.trycatchfinally'
keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI
sidebar: lb4_sidebar
editurl: https://github.com/loopbackio/loopback-next/tree/master/packages/context
permalink: /doc/en/lb4/apidocs.context.trycatchfinally.html
---

<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@loopback/context](./context.md) &gt; [tryCatchFinally](./context.trycatchfinally.md)

## tryCatchFinally() function

Try to run an action that returns a promise or a value with error and final actions to mimic `try {} catch(err) {} finally {}` for a value or promise.

**Signature:**

```typescript
export declare function tryCatchFinally<T>(action: () => ValueOrPromise<T>, errorAction?: (err: unknown) => T | never, finalAction?: () => void): ValueOrPromise<T>;
```

## Parameters

<table><thead><tr><th>

Parameter


</th><th>

Type


</th><th>

Description


</th></tr></thead>
<tbody><tr><td>

action


</td><td>

() =&gt; [ValueOrPromise](./context.valueorpromise.md)<!-- -->&lt;T&gt;


</td><td>

A function that returns a promise or a value


</td></tr>
<tr><td>

errorAction


</td><td>

(err: unknown) =&gt; T \| never


</td><td>

_(Optional)_ A function to be called once the action is rejected (synchronously or asynchronously). It must either return a new value or throw an error.


</td></tr>
<tr><td>

finalAction


</td><td>

() =&gt; void


</td><td>

_(Optional)_ A function to be called once the action is fulfilled or rejected (synchronously or asynchronously)


</td></tr>
</tbody></table>
**Returns:**

[ValueOrPromise](./context.valueorpromise.md)<!-- -->&lt;T&gt;


