Promises And Methods Flashcards

1
Q

What does Promise.all do?

A

It takes an array of promises and returns a single promise.

It will fullfill only if all the promises are fulfilled. If any one of them returns a reject then promise.all returns a reject.

If fullfilled it returns an array of all the resolve data in the same order as the array of promises input to the method.

If rejected, the rejection reason is taken from the first promise that was rejected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Does Promise.All run in sequence or concurrently?

A

Concurrently. This reduces the waiting time for the promises to complete.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax of promise.All?

A

Promise.all([promise1, promise2, promise3]).then(
(values) => {console.log(‘Promise All: ‘,values);},
(error) => {console.error(error);}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a promise?

A

Promises are used to manage asynchronous operations.
A promise represents a value that might not be available yet but will be at some point in the future,.

Promises have three states: Pending, Fulfilled, Rejected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some use cases for promises?

A

Fetching data from an APIs
Reading files
Performing I/O operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the three states of Promises?

A

Pending, Fulfilled, Rejected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does Promise.any do?

A

The Promise.any() takes an array of promises returns a single Promise. This returned promise fulfills when any of the input’s promises fulfills. Only the value of the first resolved promise is returned. It will NOT return later resolved or rejected promises.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the syntax of Promise.all?

A

Promise.any([promise4, promise5, promise6]).then(
(values) => {console.log(values);},
(error) => {console.error(error);}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does Promise.race() do?

A

The Promise.race() takes an array of promises as input and returns a single Promise.

It resolves or rejects depending on which of the promises fulfills fastest. If the one that fulfills fastest is rejected then the race method also rejects.

The difference between ‘race’ and ‘any’ is that any will fail if any one of the promises rejects while race will always resolve whether it returns a resolve or reject: you want the result of the fastest one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does Promise.allSettled do?

A

The Promise.allSettled() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input’s promises settle (including when an empty iterable is passed), with an array of objects that describe the outcome of each promise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What defines a settled promise?

A

A promise is said to be settled if it is either fulfilled or rejected, but not pending.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly