Opens in a new tab
A picture of Jeff Brigman
I CODE TODAY FOR
A BETTER TOMORROW

Glossary Term

Promise.try()

2 tags 1 category

The Promise.try() static method takes a callback of any kind (returns or throws, synchronously or asynchronously) and resolves its result to a Promise.

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 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.

You can wrap this expression in try…catch:

The problem is that try…catch is not an expression, so you can’t directly use it in expression positions like passing it to other functions.

Syntax example: Promise.try(func)

Term Connections

Explore related terms connected by shared tags, shared categories, and relationship phrases. This map loads only when it comes into view.

Loading term connections…