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");
});
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.
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.
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.
Promises deal with one asynchronous event at a time, while observables handle a sequence of asynchronous events over a period of time.
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With