简体   繁体   English

多个请求,相同的响应

[英]Multiple requests, same response

I'm facing this strange issue with the $http service: every request made to the API provided on the company that I work, wrapped in a $q.all(), return the same response. 我在$ http服务中遇到了一个奇怪的问题:对我工作的公司提供的API的每个请求(包装在$ q.all()中)都返回相同的响应。

var promises = [
    $httpPromiseA(),
    $httpPromiseB(),
    $httpPromiseC()
];

$q.all(promises)
    .then(function (response) {
        $log.log(response);
    });

// $log.log returns [expectedObjectFromA, expectedObjectFromA, expectedObjectFromA]

This occurs randomly: the expectedObjectFromA can be expectedObjectFromB or expectedObjectFromC. 这是随机发生的:ExpectedObjectFromA可以是ExpectedObjectFromB或ExpectedObjectFromC。 The fact is that all of the three objects return the same, or two of one and one of other, etc. 事实是,所有三个对象都返回相同的值,或者一个返回一个,另一个返回另一个,等等。

If I chain one after other, in a linear manner, they all work perfectly, but takes more time to acomplish the task, of course. 如果我以线性方式一个接一个地链接,它们都可以完美地工作,但是当然需要更多的时间来完成任务。

var def = $q.deferred();
var resolvedData = [];

$httpPromiseA()
    .then(function (response) {
        $log.log(response);

        resolvedData.push(reponse);

        return $httpPromiseB();
    })
    .then(function (response) {
        $log.log(response);

        resolvedData.push(reponse);

        return $httpPromiseC();
    })
    .then(function (response) {
        $log.log(response);

        resolvedData.push(reponse);

        return def.resolve(resolvedData);
    });

return def.promise();

// $log.log returns [expectedObjectFromA],
// [expectedObjectFromB] and [expectedObjectFromC]
// as expected. ResolvedData brings [expectedObjectFromA,
// expectedObjectFromB, expectedObjectFromC].

Can you give me some directions on what may be happening here? 您能给我一些可能发生的事情的指示吗?

I'm using the core implementations of $http and $q. 我正在使用$ http和$ q的核心实现。 I've tried one $q.allSettled, applied as $decorator, based on the API resource of Kris Kowalk's Q, but didn't succeed too. 我已经尝试过一个$ q.allSettled,用作$ decorator,基于Kris Kowalk Q的API资源,但也没有成功。

Thanks. 谢谢。

EDIT 1: 编辑1:

I cannot pass the arguments to the functions separately because it's a wrapper function thats call the $http service, here in my app. 我不能将参数分别传递给函数,因为它是包装函数,在我的应用程序中称为$http服务。 The wrapper function expects a String as the first argument, and an Object as the second. 包装函数期望将String作为第一个参数,将Object作为第二个参数。 The wrapper function returns the $http calls. 包装函数返回$http调用。

EDIT 2: 编辑2:

This Plunker calls 2 concurrent requests, one to the Instagram API and the other to the Flickr API. Plunker调用2个并发请求,一个向Instagram API发出,另一个向Flickr API发出。 The problem don't occur is this Plunker. 不会发生的问题是此Plunker。 I really don't know how to deal with this trouble. 我真的不知道该如何处理这个麻烦。 This is REALLY annoying. 这真是令人讨厌。

In $q.all you'll want to pass in an array of promises, instead executing the function. 在$ q.all中,您将希望传递一个promise数组,而不是执行该函数。 $httpPromiseA.$promise instead of $httpPromiseA() and so on. $ httpPromiseA。$ promise代替$ httpPromiseA()等等。

The problem was on the server side, which was having problems with simultaneous requests, answering all made together with the same response. 问题出在服务器端,服务器在同时请求时遇到问题,所有请求都以相同的响应来回答。

Thanks for all support and attention. 感谢所有支持和关注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM