Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining promises in angular

How do I join 2+ promises in angular?

(I have seen the documentation to chain them, but that's not what I need)

Example of what I want to achieve in Q:

Q.all([promiseA, promiseB]).done(function () {
    console.log("Both promises have been executed concurrently and then resolved");
});
like image 732
Razor Avatar asked Jan 22 '14 15:01

Razor


People also ask

Can we use promises in Angular?

How To Create Promises in Angular? To create a promise in Angular we just need to use 'new Promise(function)' syntax. The promise constructor takes function as a parameter and that inner function takes resolve and reject as a params.

What is Promise in Angular with example?

A promise is a placeholder for a future value. It serves the same function as callbacks but has a nicer syntax and makes it easier to handle errors.

What are promises Angular 9?

Because they are promises you need to respect their resolve time, it means to wait until they are completed and that means you can't call them as a normal function when you rely on their result. Also because it's an angular app try to convert them to rxjs stream via from function.

What is difference between Promise and observable in Angular?

Promises deal with one asynchronous event at a time, while observables handle a sequence of asynchronous events over a period of time.


1 Answers

Use $q.all()

$q.all([promise1, promise2]).then(function (result) {
// result should be an array
}, function () {
// call when either promise1 or promise 2 is rejected
});
like image 192
long.luc Avatar answered Sep 21 '22 05:09

long.luc