-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Add "any" method on $q service #11808
Comments
@candreoliveira, I think this is a bad idea. There is the .race in the ES standard, it's an better idea to implement that. I like the functionality, but if there is a documented standart, it's better to comply to that right? |
@candreoliveira I can confirm
There's #12929 for race |
@SanderElias I was wrong about the semantics of
// This code is not meant for production!
angular.module('myModule').config(function ($provide) {
$provide.decorator('$q', function ($delegate) {
$delegate.race = () => {}; // Let's assume it's implemented according to the Promise spec
$delegate.any = promises => $delegate.race([
// covers the case of "resolve when first is resolved"
$delegate.race(promises),
// covers the case of "reject when all are rejected"
$delegate((__resolve, reject) => {
const reasons = [];
const bind = (promise, promisesSize) => promise.catch(reason => {
reasons.push(reason);
if (reasons.length === promisesSize) {
reject(reasons);
}
return $delegate.reject(reason);
});
if (Array.isArray(promises)) {
const promisesSize = promises.length;
promises.forEach(promise => {
bind(promise, promisesSize);
})
} else if (typeof promises === 'object' && promises != null) {
const promisesSize = Object.getOwnPropertyNames(promises).length;
for (const promiseName in promises) {
if (promises.hasOwnProperty(promiseName)) {
bind(promises[promiseName], promisesSize);
}
}
}
}),
]);
return $delegate;
});
}); If I'm not mistaken then implementing |
We are going to implement |
$q is a service inspired by Kris Kowal's Q.
It's usual in frontend applications make requests to some APIs and based on (at least) an answer continue processing the business logic.
For example: Imagine an e-commerce application. It almost always needs to receive coupons, vouchers and/or freight discounts. A good architecture will separate responsabilities into distinct micro-services. However, the "perfect" ui could be a single input text. So, we'd use a single input to dispatch 3 requests (3 end-points: coupon, voucher and freight discount) and based on any answer apply the user discount. Hence, we need the ability to manage all requests into a single promise.
The Kris Kowal's Q has the method "any" and I think It's very helpful. What do you think about it @IgorMinar ?
The text was updated successfully, but these errors were encountered: