需要帮助来解决嵌套的Promises,Promise没有得到解决

时间:2022-05-03 20:16:45

I have a service designed which takes some parameter and then loops through input array, and find out which item is empty and then add those to and array and then after $q.all resolved it should give me the array of empty items.

我有一个服务设计,它接受一些参数,然后循环通过输入数组,找出哪个项是空的,然后添加到和数组,然后在$ q.all解决后,它应该给我空数组的数组。

input is array of items

输入是项目数组

function getElements(inputs) {
            var elements= [],
                promise, whenPromise,
                promises = [],
                mainPromise = $q.defer();
            if (inputs.length === 0) {
                mainPromise.resolve(elements);
                return mainPromise.promise;
            }

            angular.forEach(inputs, function (input) {
                promise = getPromises(input);
                whenPromise = $q.resolve(promise).then(function (response) {
                $timeout(function() {
                        if (response.isEmpty) {
                  **//perform action with the response.data;**
                        }
                  });
                }, function () {

                });

                promises.push(whenPromise);
            });


            $q.all(promises).finally(function () {
                 mainPromise.resolve(outdatedEntities);

            });

            return mainPromise.promise;
        }

        function getPromises(input) {
            var deferred = $q.defer();

            someSerivce.getItemDetails(input.Id).then(function (value) {
                if (value === null) {
                    $timeout(function () {
                        deferred.resolve({data: input, isEmpty: true});
                    });

                } else {
                    //have item locally, but need to see if it's current
                    input.isEmpty().then(function (isEmpty) {
                        $timeout(function () {
                            deferred.resolve({data: input, isEmpty: isEmpty});
                        });
                    }, function (error) {
                        deferred.reject(error);
                    });
                }

            });

            return deferred.promise;
        }

Now, the problem is the $q.all is never resolved. Even though all the internal promises are getting resolved.

现在,问题是$ q.all永远不会解决。尽管所有内部承诺都得到了解决。

1 个解决方案

#1


1  

This should work:

这应该工作:

function getElements(inputs) {
    var elements = [],
            promise, whenPromise,
            promises = [],
            mainPromise = $q.defer();
    if (inputs.length === 0) {
        mainPromise.resolve(elements);
        return mainPromise.promise;
    }

    angular.forEach(inputs, function (input) {
        promise = getPromises(input);
        whenPromise = promise.then(function (response) {

            if (response.isEmpty) {
                * *//perform action with the response.data;**
            }

        }, function () {

        });

        promises.push(whenPromise);
    });


    return $q.all(promises).finally(function () {
        return outdatedEntities;
    });
}

function getPromises(input) {
    return someSerivce.getItemDetails(input.Id).then(function (value) {
        if (value === null) {
            return {data: input, isEmpty: true};
        } else {
            //have item locally, but need to see if it's current
            return input.isEmpty().then(function (isEmpty) {
                return {data: input, isEmpty: isEmpty};
            }, function (error) {
                return error;
            });
        }
    });
}

#1


1  

This should work:

这应该工作:

function getElements(inputs) {
    var elements = [],
            promise, whenPromise,
            promises = [],
            mainPromise = $q.defer();
    if (inputs.length === 0) {
        mainPromise.resolve(elements);
        return mainPromise.promise;
    }

    angular.forEach(inputs, function (input) {
        promise = getPromises(input);
        whenPromise = promise.then(function (response) {

            if (response.isEmpty) {
                * *//perform action with the response.data;**
            }

        }, function () {

        });

        promises.push(whenPromise);
    });


    return $q.all(promises).finally(function () {
        return outdatedEntities;
    });
}

function getPromises(input) {
    return someSerivce.getItemDetails(input.Id).then(function (value) {
        if (value === null) {
            return {data: input, isEmpty: true};
        } else {
            //have item locally, but need to see if it's current
            return input.isEmpty().then(function (isEmpty) {
                return {data: input, isEmpty: isEmpty};
            }, function (error) {
                return error;
            });
        }
    });
}