Skip to content

Codebyte examples for Javascript Promise.all and finally #2601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions content/javascript/concepts/promise/terms/all/all.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: '.all()'
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."
Description: 'Returns a new promise that can be accessed as an array of resolved values of fulfilled promises.'
Subjects:
- 'Web Development'
Tags:
Expand All @@ -11,19 +11,19 @@ CatalogContent:
- 'paths/full-stack-engineer-career-path'
---

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

## Syntax

```js
```pseudo
Promise.all(iterableObject);
```

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

## Example

Working with two Promise objects, `promiseA` and `promiseB`:
Working with two promises, `promiseA` and `promiseB`:

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

The output would be:

```plaintext
Results from Promise.all(): [144,42]
```shell
Results from Promise.all(): [23,144]
Operations for Promise.all() have finished.
```

## Codebyte Example

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.

If at least one promise was rejected, `Promise.all` rejects with the value of the first rejected promise it encounters.

```codebyte/js
function getPromise(shouldResolve, value) {
return new Promise((resolve, reject) => {
if (shouldResolve) {
resolve(value);
} else {
reject(value);
}
});
}

function onResolve(value) {
console.log('Resolved: ' + value);
}

function onReject(value) {
console.log('Rejected: ' + value);
}

const resolvedPromise1 = getPromise(true, 'Resolved promise 1');
const resolvedPromise2 = getPromise(true, 'Resolved promise 2');
const rejectedPromise1 = getPromise(false, 'Rejected promise 1');
const rejectedPromise2 = getPromise(false, 'Rejected promise 2');

Promise.all([resolvedPromise1, resolvedPromise2])
.then(onResolve)
.catch(onReject);

Promise.all([rejectedPromise1, resolvedPromise1, rejectedPromise2, resolvedPromise2])
.then(onResolve)
.catch(onReject);
```
18 changes: 16 additions & 2 deletions content/javascript/concepts/promise/terms/finally/finally.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ CatalogContent:
- 'paths/full-stack-engineer-career-path'
---

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

## Syntax

```js
```pseudo
myPromiseObject.then(fulfilledPromiseCallback).finally(finalPromiseCallback);
```

Expand Down Expand Up @@ -46,3 +46,17 @@ myPromise
Operations have ended.
*/
```

## Codebyte Example

In the following example, the callback function passed to `finally()` is executed when the promise either resolves or rejects.

```codebyte/js
new Promise((resolve, reject) => resolve('Promise 1'))
.then((value) => console.log('Resolved: ' + value))
.finally(() => console.log('Always log this when the promise fulfills!'));

new Promise((resolve, reject) => reject('Promise 2'))
.catch((value) => console.log('Rejected: ' + value))
.finally(() => console.log('Just log it!'));
```