![Jeremy Wagner](https://web.dev/images/authors/jlwagner-v7.jpg) Jeremy Wagner [GitHub](https://github.com/malchata) [LinkedIn](https://www.linkedin.com/in/malchata) [Homepage](https://jlwagner.net/)

<br />


Published: January 17, 2025

<br />

> [!TIP]
> **Celebration:** This web feature is now available in all three major browser engines, and becomes **Baseline Newly available** as of January 7, 2025.

<iframe src="https:///frame/blog/promise-try-baseline/index_0cabbfa07095f75116aff857397a76a67423e2211d4ee84b0f97ab740d71f07c.frame" class="framebox inherit-locale " allow="clipboard-write https://" allowfullscreen is-upgraded></iframe>

Doing asynchronous work on the web can be challenging---and a lot of what's done on the web involves asynchronicity. Whether that involves `fetch` calls or other asynchronous operations, how you do that work is made easier by browsers when built-in methods provide robust error handling. This has already been provided for Promises using the `then`, `catch`, and `finally` methods.

Even so, the way in which you architect your application's codebase should be flexible, and not everything you'll do in your application is necessarily asynchronous. Sometimes you might want to handle the result of a callback in a way that would be simpler if it didn't matter whether a callback you passed to a `Promise` is synchronous or not. `Promise.try` is a method now available in all major browser engines that simplifies this for developers, which means that it's now Baseline Newly available.

## What is `Promise.try` and how does it work?

`Promise.try` is a convenience method that makes error handling for synchronous callback functions easier than if you used `Promise.resolve`:

```javascript
// If the callback is synchronous and it throws
// an exception, the error won't be caught here:
new Promise(resolve => resolve(callback());

// But it will be here:
Promise.try(callback);
```

Then, using the `then`, `catch`, and `finally` methods, handle the resolution or rejection of a `Promise`:

```javascript
Promise.try(callback)
  .then(result => console.log(result))
  .catch(error => console.log(error))
  .finally(() => console.log("All settled."));
```

> [!NOTE]
> Note: If using `async`/`await` syntax, you wouldn't use `Promise.try`, but a `try`/`catch`/`finally` block instead.

What if your callback function has arguments? You can handle this in one of two ways:

```javascript
// This creates an extra closure, but works:
Promise.try(() => callback(param1, param2));

// This doesn't create an extra closure, and still works:
Promise.try(callback, param1, param2);
```

The primary benefit of using `Promise.try` is that it lets you uniformly use Promises regardless of whether or not the callback you pass to it is synchronous or asynchronous. This means that, in the case of a utility function that passes callbacks to a `Promise` that's widely used in your code, using `Promise.try` ensures proper error handling for any callback you pass to it. For additional information and other potential concerns or use cases, consult [the MDN documentation for `Promise.try`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise/try).

## Conclusion

Now that `Promise.try` has reached Baseline Newly available, you should be able to use it in all major browser engines. As time passes, you should expect to be able to use `Promise.try` in your web applications with increasing confidence that it will be a stable and interoperable part of the web platform.