Skip to content

Commit 248d224

Browse files
carlos-dscaupolicandiazSSwiniarski
authored
Codebyte examples for Javascript Promise.all and finally (#2601)
* #2510 Add Codebyte example for Javascript - Promise.all * #2510 Add Codebyte example for Javascript - Promise - finally * review edits * example typo --------- Co-authored-by: Caupolican Diaz <caupolicandiaz@gmail.com> Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com>
1 parent 970281f commit 248d224

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

content/javascript/concepts/promise/terms/all/all.md

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: '.all()'
3-
Description: "Returns a new Promise that can be accessed as an array of resolved values of fulfulled Promises. It takes an iterable object, such as an Array, that contains one or more Promise objects. This is ideal when working with Promises that depend on one another's completion."
3+
Description: 'Returns a new promise that can be accessed as an array of resolved values of fulfilled promises.'
44
Subjects:
55
- 'Web Development'
66
Tags:
@@ -11,19 +11,19 @@ CatalogContent:
1111
- 'paths/full-stack-engineer-career-path'
1212
---
1313

14-
The `.all()` method returns a new Promise that can be accessed as an array of resolved values of fulfulled Promises. It takes an iterable object, such as an `Array`, that contains one or more Promise objects. This is ideal when working with Promises that depend on one another's completion.
14+
The **`.all()`** method returns a new `Promise` object that can be accessed as an array of resolved values of fulfilled promises. It takes an iterable object, such as an `Array`, that contains one or more `Promise` objects. This is ideal when working with promises that depend on one another's completion.
1515

1616
## Syntax
1717

18-
```js
18+
```pseudo
1919
Promise.all(iterableObject);
2020
```
2121

22-
The `iterableObject` is usually an array of Promise objects. If the array is empty, a Promise object that resolves into an empty array will be returned.
22+
The `iterableObject` is usually an array of `Promise` objects. If the array is empty, a `Promise` object that resolves into an empty array will be returned.
2323

2424
## Example
2525

26-
Working with two Promise objects, `promiseA` and `promiseB`:
26+
Working with two promises, `promiseA` and `promiseB`:
2727

2828
```js
2929
const promiseA = new Promise((resolve, reject) => {
@@ -55,7 +55,46 @@ Promise.all([promiseA, promiseB])
5555

5656
The output would be:
5757

58-
```plaintext
59-
Results from Promise.all(): [144,42]
58+
```shell
59+
Results from Promise.all(): [23,144]
6060
Operations for Promise.all() have finished.
6161
```
62+
63+
## Codebyte Example
64+
65+
The following example demonstrates that the promise returned from `Promise.all` resolves only if all promises (passed as an array) resolve. The resolved value is an array containing the values of each resolved promise.
66+
67+
If at least one promise was rejected, `Promise.all` rejects with the value of the first rejected promise it encounters.
68+
69+
```codebyte/js
70+
function getPromise(shouldResolve, value) {
71+
return new Promise((resolve, reject) => {
72+
if (shouldResolve) {
73+
resolve(value);
74+
} else {
75+
reject(value);
76+
}
77+
});
78+
}
79+
80+
function onResolve(value) {
81+
console.log('Resolved: ' + value);
82+
}
83+
84+
function onReject(value) {
85+
console.log('Rejected: ' + value);
86+
}
87+
88+
const resolvedPromise1 = getPromise(true, 'Resolved promise 1');
89+
const resolvedPromise2 = getPromise(true, 'Resolved promise 2');
90+
const rejectedPromise1 = getPromise(false, 'Rejected promise 1');
91+
const rejectedPromise2 = getPromise(false, 'Rejected promise 2');
92+
93+
Promise.all([resolvedPromise1, resolvedPromise2])
94+
.then(onResolve)
95+
.catch(onReject);
96+
97+
Promise.all([rejectedPromise1, resolvedPromise1, rejectedPromise2, resolvedPromise2])
98+
.then(onResolve)
99+
.catch(onReject);
100+
```

content/javascript/concepts/promise/terms/finally/finally.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ CatalogContent:
1111
- 'paths/full-stack-engineer-career-path'
1212
---
1313

14-
Returns a new `Promise` object after the previous Promise in the chain has been resolved or rejected. This last part of the chain will execute no matter what.
14+
Returns a new `Promise` object after the previous promise in the chain has been resolved or rejected. This last part of the chain will execute no matter what.
1515

1616
## Syntax
1717

18-
```js
18+
```pseudo
1919
myPromiseObject.then(fulfilledPromiseCallback).finally(finalPromiseCallback);
2020
```
2121

@@ -46,3 +46,17 @@ myPromise
4646
Operations have ended.
4747
*/
4848
```
49+
50+
## Codebyte Example
51+
52+
In the following example, the callback function passed to `finally()` is executed when the promise either resolves or rejects.
53+
54+
```codebyte/js
55+
new Promise((resolve, reject) => resolve('Promise 1'))
56+
.then((value) => console.log('Resolved: ' + value))
57+
.finally(() => console.log('Always log this when the promise fulfills!'));
58+
59+
new Promise((resolve, reject) => reject('Promise 2'))
60+
.catch((value) => console.log('Rejected: ' + value))
61+
.finally(() => console.log('Just log it!'));
62+
```

0 commit comments

Comments
 (0)