Standard built-ins

Promise.try()

Try the free JavaScript compiler

Invokes the callback immediately with supplied arguments and resolves or rejects a returned promise using its completion. Not available in the local Node 22.16.0 runtime; the example only tests the fallback.

Formal Syntax & Structure

javascript syntaxjavascript
Promise.try(callback, ...args)

Parameters & Attributes

NameTypeDescription
callbackfunctionFunction to invoke.
argsany[]Arguments forwarded to the callback.

Return Value

Promise

Promise resolved to return value or rejected with thrown error.

Working Example

Example Snippetjavascript
if (typeof Promise.try === "function") {  Promise.try(() => { console.log("call"); throw new Error("oops"); }).catch(e => console.log(e.message));  console.log("after");} else { console.log("Promise.try unavailable"); }
JavaScript Reference