加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – $q.all慢于顺序.then()?

发布时间:2020-12-17 18:06:15 所属栏目:安全 来源:网络整理
导读:我有一些角度代码,通过$http.get调用两个单独的后端服务.后端是ASP.NET MVC 5. 我通过$http.get调用服务,因为在继续之前需要来自两个服务的响应,我将返回的promise包装在$q.all中.但是,与通过$q.all解析promise相比,顺序解析promises(即在第一个promise的.th
我有一些角度代码,通过$http.get调用两个单独的后端服务.后端是ASP.NET MVC 5.

我通过$http.get调用服务,因为在继续之前需要来自两个服务的响应,我将返回的promise包装在$q.all中.但是,与通过$q.all解析promise相比,顺序解析promises(即在第一个promise的.then回调中调用第二个服务)相比,似乎存在巨大的开销.

开销出现在TTFB(第一个字节的时间).

我无法弄清楚为什么$q.all会比在开始下一个之前顺序等待一个承诺解决的速度慢.事实上,我认为$q.all会更快,因为它允许我在第一个服务调用之前启动第二个服务调用.

继续阅读实施细节.

这些后端服务相当轻量级:

ProductsController的:

[HttpGet]
public Dictionary<string,PriceListTypeDto> GetPriceListTypesForProducts([FromUri] List<string> productErpIds)
{
    // Work to get PriceListTypes. Work takes 40 ms on avg.
}

UserController的:

[HttpGet]
public int? GetUserOrganizationId()
{
    // work to get orgId. 1-10 ms runtime on avg.
}

调用这些服务的Javascript函数:

var addPriceListTypes = function (replacementInfoObjects,productErpIds) {
    return productService.getPriceListTypesForProducts(productErpIds) // Returns promise from $http.get
        .then(function (response) {
            // Simple work,takes 1 ms.
        })
        .catch(function () {
        });
}


var addOrganizationSpecificDetails = function (replacementInfoObjects) {
    return userContextService.getUserOrganizationId() // Returns promise from $http.get
       .then(function (response) {
            // Simple work,takes 1 ms.
        })
        .catch(function () {
        });
};

处理承诺:

选项1:在调用$q.all.then之前需要大约600毫秒.

映射-service.js:

var deferredResult = $q.defer();
var orgDetailsPromise = addOrganizationSpecificDetails(productInfoObjects);
var priceListPromise = addPriceListTypes(products,productErpIds);

$q.all([orgDetailsPromise,priceListPromise])
    .then(function () {
        deferredResult.resolve(productInfoObjects);
    }).catch(function () {
        deferredResult.reject();
    });

return deferredResult.promise;

Chrome devtools的性能:

选项2:在两个承诺解决之前需要大约250毫秒:

映射-service.js:

var deferredResult = $q.defer();
addOrganizationSpecificDetails(productInfoObjects)
    .then(function () {
        addPriceListTypes(productInfoObjects,productErpIds)
            .then(function () {
                deferredResult.resolve(productInfoObjects);
            })
            .catch(function () {
                deferredResult.reject();
            });
    })
    .catch(function () {
        deferredResult.reject();
    });

return deferredResult.promise;

Chrome devtools的性能:

选项1中的开销来自哪里?我错过了什么?我完全被这里难住了.如果您需要更多信息,请与我们联系.

解决方法

我在前一段时间为Microsoft CRM构建自定义屏幕时遇到了类似的问题.我正在使用$q.all()并意识到通过同时向多个请求命中服务器,其中一些失败或花了很长时间才得到解决.最终我们做了同样的事情 – 链接请求而不是一次性解决所有请求.

我相信这可能与我们的问题类似.我想说的是,我对这种情况并不感到惊讶.我不确定我们的问题究竟是什么(意味着什么导致了它),但它在那里并且不在我们手中(它是在线CRM,而不是托管).

我知道我的答案并没有真正提供任何解决方案,但我认为这是一种可以让您安心的见解.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读