JavaScript Promise try()
Description
Promise.try() executes a given
function and wraps its result in a promise.
Promise.try() is particularly useful for unifying error handling
and ensuring consistent promise-based workflows, whether the executed function is synchronous
or asynchronous.
Example
// Synchronous function that might throw an error
function syncOperation(shouldThrow) {
if (shouldThrow) {
throw new Error("Synchronous error!");
}
return "Synchronous success";
}
// Asynchronous function
async function asyncOperation() {
await new Promise(resolve => setTimeout(resolve, 100));
return "Asynchronous success";
}
// Using Promise.try() with a synchronous function
Promise.try(() => syncOperation(false))
.then(result => console.log("Sync success:", result))
.catch(error => console.error("Sync error:", error.message));
Promise.try(() => syncOperation(true))
.then(result => console.log("Sync success:", result))
.catch(error => console.error("Sync error:", error.message));
// Using Promise.try() with an asynchronous function
Promise.try(() => asyncOperation())
.then(result => console.log("Async success:", result))
.catch(error => console.error("Async error:", error.message));
In the example above, Promise.try() ensures that both synchronous and asynchronous operations, including synchronous errors, are handled within a promise context, for a streamlined and robust error management strategy.
Note
If you are using async/await syntax, you should use the standard try/catch blocks instead, as they provide similar unified error handling for asynchronous code.
Syntax
Promise.try(function, arg1,...,argN)
Parameters
| Parameter | Description |
| function | Function that is called asynchronusly |
| arg1,...,argN | Arguments passed to the function |
Return Value
| Type | Description |
| Object |
A new Promise Object. Fulfilled, if func synchronously returns a value. Rejected, if func synchronously throws an error. Fulfilled or rejected, if func returns a promise. |
Browser Support
Promise.try() is a JavaScript 2025 feature.
ES 2025 is supported in all modern browsers since May 2025:
| Chrome 136 |
Edge 136 |
Firefox 129 |
Safari 18.2 |
Opera 120 |
| Apr 2025 | Apr 2025 | Aug 2024 | Des 2024 | May 2025 |