等待所有承诺解决 and/or 拒绝?

Wait for all promissed to resolve and/or reject?

我需要 rejection/resolution 值的数组,用于并行发出的多个请求。 Angular $q 不提供此类选项($q.all 将 return 值数组仅当每个请求都已解决!)。

有没有 Angular 的方法,或者我应该寻找一些第 3 方承诺处理库,比如原始 Q?

据我所知,Angular $q 服务不提供此功能。然而,它在 Kris Kowal 的(原始)Q 中可用 allSettled

示例来自 the docs

Q.allSettled(promises)
.then(function (results) {
    results.forEach(function (result) {
        if (result.state === "fulfilled") {
            var value = result.value;
        } else {
            var reason = result.reason;
        }
    });
});

您可以查看 the source code 了解此方法并在 Angular 中自己实施

/**
 * Turns an array of promises into a promise for an array of their states (as
 * returned by `inspect`) when they have all settled.
 * @param {Array[Any*]} values an array (or promise for an array) of values (or
 * promises for values)
 * @returns {Array[State]} an array of states for the respective values.
 */
Promise.prototype.allSettled = function () {
    return this.then(function (promises) {
        return all(array_map(promises, function (promise) {
            promise = Q(promise);
            function regardless() {
                return promise.inspect();
            }
            return promise.then(regardless, regardless);
        }));
    });
};

但我的建议是使用标准 Q 库并将其包装在 Angular 服务中。