You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`Promise.try()` is a method that executes a given function, whether it's synchronous or asynchronous, and wraps the result in a promise. If the function throws an error or returns a rejected promise, `Promise.try()` will return a rejected promise. If the function completes successfully, the returned promise will be fulfilled with its value.
267
267
@@ -286,6 +286,24 @@ Promise.try(mightThrow)
286
286
287
287
In this example, `Promise.try()` ensures that if `mightThrow()` throws an error, it will be caught in the `.catch()` block, making it easier to handle both sync and async errors in one place.
288
288
289
+
### **`Promise.withResolvers()`**:
290
+
291
+
This method creates a new promise along with its associated resolve and reject functions, and returns them in a convenient object. This is used, for example, when you need to create a promise but resolve or reject it later from outside the executor function.
In this example, `Promise.withResolvers()` gives you full control over when and how the promise is resolved or rejected, without needing to define the executor function inline. This pattern is commonly used in event-driven programming, timeouts, or when integrating with non-promise-based APIs.
306
+
289
307
## Error Handling with Promises
290
308
291
309
Handling errors in Promises ensures your application behaves correctly in case of unexpected situations.
@@ -375,7 +393,7 @@ In short, the execution order is as follows:
375
393
If a microtask queues another microtask, the new one is also executed before moving on.
0 commit comments