首页 \ 问答 \ 嵌套的$ http调用$ q.all返回promises但未解决(Nested $http calls $q.all returns promises but not resolved)

嵌套的$ http调用$ q.all返回promises但未解决(Nested $http calls $q.all returns promises but not resolved)

我调用一个函数返回一个ID,我想在链式调用中使用它。 一切似乎都有效,直到我想要阅读所有返回的对象..这些是承诺,但我无法找出为什么我无法解决它们。

        //Get bubbles and then it calls another function getBubbleMessage with result from previous and last getBubbleMessage returns an array of promises. 

   $scope.loadStartPage = function () {

        $scope.getBubblesThatUserHasAccessTo().then($scope.getBubbleMessage).then(function (data) {
            $log.info("INFO:: " + data);
            $scope.bubblesWithMessages = data;


        });
    };

    $scope.getBubblesThatUserHasAccessTo = function () {
        var deferred = $q.defer();

        BubblesService.getBubblesUserAccess().then(function (result) {
            deferred.resolve(result);

        });

        return deferred.promise;
    };

这个函数得到了一些我们需要解决的消息,这些消息连接到那些以上服务返回的id:s

    $scope.getBubblesThatUserHasAccessTo = function () {
        var deferred = $q.defer();

        BubblesService.getBubblesUserAccess().then(function (result) {
            deferred.resolve(result);

        });

        return deferred.promise;
    };

这个函数获取所有消息并返回promise对象 - 这些我无法解决?

    $scope.getBubbleMessage = function (data) {

        var deferred = $q.defer();
        var promises = [];

        angular.forEach(data, function (item) {
            $log.info("DDD" + item.name);
            var promise = BubblesService.getBubbleMessages(item.id, 0, 1);
            promises.push(promise);
        });

        //return $q.all([promises]);
        $q.all([promises]).then(function (result) {
            $log.info(result);
            return result[0];

        });

    };

以上函数返回一个包含60个对象的数组。 在此处输入图像描述

最后我想要一个新的对象,我在页面上的ng-repeat中使用。 我真的认为这是对角度和承诺不熟悉的东西......但经过几个小时的尝试解决这个问题我真的需要帮助:)


I call a function that returns a list with ID that i want to use in chained call. Everything seems to work until i want to read all those objects that are returned.. Those are promises but i cannot find out why i cannot resolve them.

        //Get bubbles and then it calls another function getBubbleMessage with result from previous and last getBubbleMessage returns an array of promises. 

   $scope.loadStartPage = function () {

        $scope.getBubblesThatUserHasAccessTo().then($scope.getBubbleMessage).then(function (data) {
            $log.info("INFO:: " + data);
            $scope.bubblesWithMessages = data;


        });
    };

    $scope.getBubblesThatUserHasAccessTo = function () {
        var deferred = $q.defer();

        BubblesService.getBubblesUserAccess().then(function (result) {
            deferred.resolve(result);

        });

        return deferred.promise;
    };

This function is gettings some things that we need to resolve messages connected to those id:s that above service is returning

    $scope.getBubblesThatUserHasAccessTo = function () {
        var deferred = $q.defer();

        BubblesService.getBubblesUserAccess().then(function (result) {
            deferred.resolve(result);

        });

        return deferred.promise;
    };

This function get alls messages and returns promise objects - and these i cannot resolve??

    $scope.getBubbleMessage = function (data) {

        var deferred = $q.defer();
        var promises = [];

        angular.forEach(data, function (item) {
            $log.info("DDD" + item.name);
            var promise = BubblesService.getBubbleMessages(item.id, 0, 1);
            promises.push(promise);
        });

        //return $q.all([promises]);
        $q.all([promises]).then(function (result) {
            $log.info(result);
            return result[0];

        });

    };

Above function returns an array of 60 objects.. enter image description here

In the end i want to have a new object that i use in my ng-repeat on page. I really think this is something todo that im new to angular and promises.... but after a couple of hours of trying to fix this i really need help :)


原文:https://stackoverflow.com/questions/30326143
更新时间:2020-10-17 12:10

最满意答案

$ q.all采取了一系列承诺。 在这里,你正在做$ q.all([myPromises]),它会立即解决,因为'[myPromise]'是一个数组而不是一个promise(你给第一个数组参数,唯一的元素就是一个承诺数组)应该只使用promise数组。所以[]而不是[[]])。 第二个问题:您没有在父函数中返回此承诺。

你应该简单地改变块

$q.all([promises]).then(function (result) {
            $log.info(result);
            return result[0];

        });

return $q.all(promises);

哪个将通过为数组中的每个promise解析的数组来解析。


$q.all takes an array of promise. Here, you are doing $q.all([myPromises]), which resolve instantly, because '[myPromise]' is an array and not a promise (you give an array parameter with first and only element is an array of promise when you should simply use the promise array. So [] and not [[]]). Second issue : you are not returning this promise in the parent function.

You should simply change the block

$q.all([promises]).then(function (result) {
            $log.info(result);
            return result[0];

        });

with

return $q.all(promises);

Which will resolve with an array of resolved for each promise in the array.

2015-05-19

相关问答

更多

使用$ q.all链接Angular中的承诺(Chaining promises in Angular with $q.all)

目前你的内部promises ($ q.all promise)不是从promise变量返回的。 您还应该返回promises (复数),以确保链应该起作用。 this.getData = function(params) { var promise = $http({}).then(function(firstdata) { // creating new promise array in `promises` function var promises ...

angularjs $ q.all用于嵌套承诺(angularjs $q.all for nested promises)

您的requests数组应该具有在将其传递给$q.all时需要解决的所有承诺。 目前,您正在将先前承诺解决后的loadEpisodes承诺添加到requests 。 你可以解决这个问题,以便内部部分返回它自己的$.q.all ,它将被链接: var requests = []; series.forEach(function (seriesId) { requests.push(loadSeriesDetails(seriesId).then(function ( ...

$ q.all在退货前解决($q.all is resolving before the returns)

在做了一些研究并考虑@charlietfl的各种注释后,我能让$ q.all正常工作的唯一方法就是将函数调用赋给变量然后将其推送到传递给q的数组中。 。所有。 for (var i in result.photos) { var output = doPhotos(result, i); doPhotosArray.push(output); } 我用于参考的各种示例似乎表明我原始问题中的代码应该可以工作但不能。 After doing some research and th ...

$ q.all()在角度不能很好地解决($q.all() in angular does not resolve well)

您没有正确使用承诺,因为您使用延期,这是违反承诺链的反模式。 不要使用延迟,只需为每个动作获取承诺,然后使用$ q将它们合并: var PBpromise = $http.get('/pendingBills').then(function(response) { return ['id', 'path', 'reservas', 'importe', 'fecha']; // this will return a promise with the array as the resolve v ...

$ q.all对所有值返回undefined($q.all returns undefined for all values)

你不应该对get方法返回的promise对象做.promise处理,因为你已经返回promise form get方法。 $q.all([a, b, c]) 当你正在做a.promise , b.promise和c.promise它们全部变成undefined ,然后$q.all数组变成$q.all([undefined, undefined, undefined])将它们传递给$q.all将给undefined结果。 You should not be doing .promise on pr ...

当某些承诺失败时如何继续使用$ q.all()(how to continue with $q.all() when some promises fail)

一个简单的黑客可能会将catch块添加到返回null的promise中,并且过滤来自promise.all结果的null结果,如下所示: let items = ["URL1", "URL2", "URL3"] , promises = items.map(url => $http.get(url).catch(e => null)) , all = $q.all(promises).then(data => data.filter(d => !!d)) all.then(data => ...

Angular Promises使用$ q.all(Angular Promises using $q.all)

你应该在map()回调中放一个return 。 var itemProgress = []; var promises = currentBatches.map(function(batch){ // return the following promise return HttpWrapper.send('/api/'+batch.job_id+'/progress', { "operation": 'GET' }) .then(function(result) { ...

承诺 - 我在$ q.all之后得到了相同的结果(Promises - I get the same result after $q.all)

试试这种方式: return WordPress.getAllCategories() .then(function (cats) { $q.all(cats.data.map(function (cat) { return WordPress.getLatestPostOfCategory(cat.id) .then(function (post) { return WordPress.getMediaById(post.data.featured_m ...

AngularJS承诺$ q.all和SignalR(AngularJS Promises $q.all and SignalR)

我的朋友Yan Yankowski为AngularJS写了一个很棒的SignalR包装器。 你可以从GitHub 下载它,这个包装器也使用$ q promises来获得结果。 例: hubFactory.getHub("myHub").run("myMethod", param_1, param_2, .... param_n).then ( function(responseData) {} ) It looks like the problem was related to the prom ...

嵌套的$ http调用$ q.all返回promises但未解决(Nested $http calls $q.all returns promises but not resolved)

$ q.all采取了一系列承诺。 在这里,你正在做$ q.all([myPromises]),它会立即解决,因为'[myPromise]'是一个数组而不是一个promise(你给第一个数组参数,唯一的元素就是一个承诺数组)应该只使用promise数组。所以[]而不是[[]])。 第二个问题:您没有在父函数中返回此承诺。 你应该简单地改变块 $q.all([promises]).then(function (result) { $log.info(result); ...

相关文章

更多

最新问答

更多
  • 如何在CSS中进行自定义文本编辑并在学校项目中获取错误(How to do custom text editing in CSS and getting a error in a school project)
  • 获取列表中最长的元素(Get the longest element of a list)
  • Files.copy(Path,Path)是否创建目录?(Does Files.copy(Path,Path) create directories?)
  • 如何在ajax请求完成后1秒递归调用函数?(How to recursively call a function 1 second after an ajax request is done?)
  • 如何统计销售量,城市,商品使用情况的客户oracle(How to statistics sales, city, customer of goods use oracle)
  • 无法从命令行覆盖pom maven-surefire-plugin(Cannot overwrite pom maven-surefire-plugin from command line)
  • 根据Select2 4.0中的值应用css类(Applying css class according to value in Select2 4.0)
  • 使用Jackson(Polymorphic)进行反序列化时缺少字段(Missing field when deserializing using Jackson (Polymorphic))
  • 使用PHP的即时警报通知功能(Instant Alert notification feature using PHP)
  • 与以前的密码相比(Comparing with previous passwords)
  • 从现金流中获取特定行(Getting specific line from Cash Flow)
  • Citrix:无法使用MDKToolkit包装apk(Citrix: can't wrap apk using MDKToolkit)
  • 流星角色包的灵活性如何?(How flexible is meteor-roles package?)
  • 带有多个按钮的Google扩展程序(Google Extension with more than one button in the toolbar)
  • php setTimezone同时出现不同的结果(php setTimezone different result for the same time)
  • 搜索不使用jQuery调用json(Search not working using jQuery with call to json)
  • 如何打印文件的内容?(How do I print out the contents of a file? C++ File Stream)
  • Django自定义包含标签(Django Custom Inclusion Tags)
  • Java Jtree mouseEvent如果没有选择任何内容(Java Jtree mouseEvent if nothing is selected)
  • 通过哈希对象循环获取次数(Get number of times in loop over Hash object)
  • Python转义序列复杂输出(Python escape sequence complex output)
  • 将列表对象中的字符串转换为逗号分隔(Convert string in list objects to comma separated)
  • 如何限制特定字符在laravel 5.4中的字符串?(How to limit string in laravel 5.4 from specific char?)
  • 如何从浏览器调用appengine servlet(how to call appengine servlet from browser)
  • Excel条件格式更改整个行颜色(Excel conditional formatting to change entire row color)
  • 在JTable中显示数据库中的数据(Displaying data from database in JTable)
  • 在android中附加文件的问题(Issue on appending a file in android)
  • 在数组中输入值(Make in input value as in array)
  • Haml + ActionMailer - Rails?(Haml + ActionMailer - Rails?)
  • 在tomcat上部署的grails war没有连接到数据库(grails war deployed on tomcat is not connecting to database)