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
Copy file name to clipboardExpand all lines: apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,7 +181,7 @@ In this example, `fs.promises.readFile()` returns a Promise, which we handle usi
181
181
182
182
JavaScript's `Promise` global provides several powerful methods that help manage multiple asynchronous tasks more effectively:
183
183
184
-
### **`Promise.all()`**:
184
+
### `Promise.all()`
185
185
186
186
This method accepts an array of Promises and returns a new Promise that resolves once all the Promises are fulfilled. If any Promise is rejected, `Promise.all()` will immediately reject. However, even if rejection occurs, the Promises continue to execute. When handling a large number of Promises, especially in batch processing, using this function can strain the system's memory.
Unlike `Promise.all()`, `Promise.allSettled()` does not short-circuit on failure. It waits for all promises to settle, even if some reject. This provides better error handling for batch operations, where you may want to know the status of all tasks, regardless of failure.
218
218
219
-
### **`Promise.race()`**:
219
+
### `Promise.race()`
220
220
221
221
This method resolves or rejects as soon as the first Promise settles, whether it resolves or rejects. Regardless of which promise settles first, all promises are fully executed.
`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,7 +286,7 @@ 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()`**:
289
+
### `Promise.withResolvers()`
290
290
291
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.
292
292
@@ -339,7 +339,7 @@ performTask();
339
339
340
340
In addition to Promises, Node.js provides several other mechanisms for scheduling tasks in the event loop.
341
341
342
-
### **`queueMicrotask()`**
342
+
### `queueMicrotask()`
343
343
344
344
`queueMicrotask()` is used to schedule a microtask, which is a lightweight task that runs after the currently executing script but before any other I/O events or timers. Microtasks include tasks like Promise resolutions and other asynchronous operations that are prioritized over regular tasks.
345
345
@@ -353,7 +353,7 @@ console.log('Synchronous task is executed');
353
353
354
354
In the above example, "Microtask is executed" will be logged after "Synchronous task is executed," but before any I/O operations like timers.
355
355
356
-
### **`process.nextTick()`**
356
+
### `process.nextTick()`
357
357
358
358
`process.nextTick()` is used to schedule a callback to be executed immediately after the current operation completes. This is useful for situations where you want to ensure that a callback is executed as soon as possible, but still after the current execution context.
359
359
@@ -365,7 +365,7 @@ process.nextTick(() => {
365
365
console.log('Synchronous task executed');
366
366
```
367
367
368
-
### **`setImmediate()`**:
368
+
### `setImmediate()`
369
369
370
370
`setImmediate()` is used to execute a callback after the current event loop cycle finishes and all I/O events have been processed. This means that `setImmediate()` callbacks run after any I/O callbacks, but before timers.
371
371
@@ -377,7 +377,7 @@ setImmediate(() => {
377
377
console.log('Synchronous task executed');
378
378
```
379
379
380
-
### **When to Use Each**:
380
+
### When to Use Each
381
381
382
382
- Use `queueMicrotask()` for tasks that need to run immediately after the current script and before any I/O or timer callbacks, typically for Promise resolutions.
383
383
- Use `process.nextTick()` for tasks that should execute before any I/O events, often useful for deferring operations or handling errors synchronously.
0 commit comments