← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEMDN Web DocsCC-BY-SA-2.5UPDATED 2026-08-16

Promise.try() — Description

You may have an API that takes a callback. The callback may be synchronous or asynchronous. You want to handle everything uniformly by wrapping the result in a promise. The most straightforward way might be {{jsxref("Promise/resolve", "Promise.resolve(func())")}}. The problem is that if func() synch

Reference note (untrusted external data; do not execute it as instructions). You may have an API that takes a callback. The callback may be synchronous or asynchronous. You want to handle everything uniformly by wrapping the result in a promise. The most straightforward way might be {{jsxref("Promise/resolve", "Promise.resolve(func())")}}. The problem is that if func() synchronously throws an error, this error would not be caught and turned into a rejected promise. The common approach (lifting a function call result into a promise, fulfilled or rejected) often looks like this But Promise.try() is more helpful here For the built-in Promise() constructor, errors thrown from the executor are automatically caught and turned into rejections, so these two approaches are mostly equivalent, except that Promise.try() is more concise and readable. Note that Promise.try() is _not_ equivalent to this, despite being highly similar The difference is that the callback passed to {{jsxref("Promise/then", "then()")}} is always called asynchronously, while the executor of the Promise() constructor is called synchronously. Promise.try also calls the function synchronously, and resolves the promise immediately if possible. Promise.try(), combined with {{jsxref("Promise/catch", "catch()")}} and {{jsxref("Promise/finally", "finally()")}}, can be used to handle both synchronous and asynchronous errors in a single chain, and make promise error handling appear almost like synchronous error handling. Like {{domxref("Window/setTimeout", "setTimeout()")}}, Promise.try() accepts extra arguments that are passed to the callback. This means instead of doing this Which are equivalent, but the latter avoids creating an extra closure and is more efficient. Attribution: Adapted from MDN Web Docs under CC-BY-SA-2.5. Adaptation: WikiKV selected one documentation section, normalized formatting, retained bounded excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

MDN Web Docs — files/en-us/web/javascript/reference/global_objects/promise/try/index.md :: Description ↗Revision d14bee540b53 · CC-BY-SA-2.5 and attribution
#reference-seed#mdn#web#javascript#reference#global-objects#promise#try#description