链接后续承诺并等待所有解决方案 [英] Chaining subsequent promises and wait for all to resolve

查看:24
本文介绍了链接后续承诺并等待所有解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些关于 Promise 的文章和问题,但我仍然无法理解它们.这是我想要做的:

我的应用程序应该从设备请求属性.属性可以表示为单个值或数组.每个属性都有属性,可以为我提供有关它的信息(例如它是数组还是可写等).

所以我的第一次尝试是首先读取属性的属性.所以我可以看看我是否需要读取单个值或数组(例如).

一切都在使用 websockets,所以我没有使用 angulars $http 服务.

var valueRequests = [];angular.forEach(properties, function (property, key) {requestPropertyAttributes(property).then(function (result) {如果(结果.isList){valueRequests.push(requestPropertyList(property));}别的 {valueRequests.push(requestPropertyValue(property));}});});$q.all(valueRequests).then(function () {//做某事.读取所有数据后});

好吧,我得到的只是一些错误(它读取列表中的值和/或反之亦然)并且 $q.all() 不会触发.

我的请求函数正在返回承诺.

这是正确的尝试还是我在某处失败了?如前所述,我对 Promise 不太确定,所以我可能无法理解这里的机制.

感谢您的帮助:)

解决方案

您需要在第一个 then 中返回内部承诺,然后将该承诺添加到列表中.

>

var valueRequests = [];angular.forEach(properties, function (property, key) {valueRequests.push(requestPropertyAttributes(property).then(function (result) {如果(结果.isList){返回 requestPropertyList(property);}别的 {返回 requestPropertyValue(property);}}));});$q.all(valueRequests).then(function () {//做某事.读取所有数据后});

I have already read some articles and questions about promises but I still have problems to get my head around them. Here's what I want to do:

My application should request properties from a device. The properties can be represented as single value or as an Array. Every property has attributes, that give me information about it (like is it an array, is it writeable, etc.).

So my first attempt was to read the attributes of an property first. So I can see if I need to read a single value or an Array (for example).

Everything is working with websockets, so I'm not using angulars $http service.

var valueRequests = [];

angular.forEach(properties, function (property, key) {

    requestPropertyAttributes(property).then(function (result) {

        if (result.isList) {

            valueRequests.push(requestPropertyList(property));
        }
        else {

            valueRequests.push(requestPropertyValue(property));
        }
    });
});

$q.all(valueRequests).then(function () {

    //do sth. after all data was read
});

Well, all I got with this are some errors (it reads list where are values and/or vice versa) and $q.all() does not fire.

My request Functions are returning promises.

Is this the correct attempt or have I failed somewhere? As said I'm not really sure with promises, so I may have a problem of understanding the mechanism here.

Thanks for any help :)

解决方案

You need to do a return of the inner promise inside the first then, and then add that promise to the list.

var valueRequests = [];

angular.forEach(properties, function (property, key) {

    valueRequests.push(requestPropertyAttributes(property).then(function (result) {

        if (result.isList) {

            return requestPropertyList(property);
        }
        else {

            return requestPropertyValue(property);
        }
    }));
});

$q.all(valueRequests).then(function () {

    //do sth. after all data was read
});

这篇关于链接后续承诺并等待所有解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆