# Promise.try()

> The `Promise.try()` static JS method calls a function and wraps its result or thrown error in a `Promise`.

## What does Promise.try() do in JavaScript?

The static method that executes a callback and always returns a `Promise`, regardless of whether the callback is sync or async. Synchronous errors become rejections, which allows handling everything with a single `catch()`.

- Topic: Promise
- Type: method
- Browser support: Chrome 128+, Chrome Android 128+, Edge 128+, Firefox 134+, Firefox Android 134+, Safari 18.2+, Safari iOS 18.2+

## Example

Wrong:

```js
function load(key) {
  return Promise.try(readCache(key))
    .catch(() => null);
}
```

Right:

```js
function load(key) {
  return Promise.try(readCache, key)
    .catch(() => null);
}
```

Passing the function and its argument lets `Promise.try()` call it, so a sync throw becomes a rejection handled by `catch()`.

## Common mistakes

- Calling the function inside the argument, so a sync error is thrown before the promise exists.

## Key points

- The function itself is passed, `Promise.try(fn)`, not its result, `Promise.try(fn())`.
- Arguments are passed after the function, as in `Promise.try(fn, a, b)`.
- `Promise.try()` replaces wrappers like `new Promise((r) => r(fn()))`.

## Interview questions

### What is the difference between `Promise.try(fn)` and `Promise.resolve().then(fn)`?

`Promise.try()` calls the function right away, synchronously. `.then(fn)` delays the call to a microtask, which changes execution order.

### Is `Promise.try()` the same as an async function?

Close: both turn sync errors into rejections. `Promise.try()` does it inline for an existing function, without writing an async wrapper.

## Related cards

- [Promise](https://wonderium.dev/js/promise.md)
- [Promise.resolve()](https://wonderium.dev/js/promise-resolve.md)
- [.catch()](https://wonderium.dev/js/catch.md)
- [async / await](https://wonderium.dev/js/async-await.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/promise-try
- Last updated: 2026-11-25
