angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

$http: multiple requests, same response

Open mateusleon opened this issue 12 years ago • 9 comments

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.

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. 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. I've tried one $q.allSettled, applied as $decorator, based on the API resource of Kris Kowalk's Q, but didn't succeed too.

Thanks.

mateusleon avatar Oct 08 '14 03:10 mateusleon

Hi, I removed the fix from the title. This format is only for PR Without a reduction we cannot really say what this is about. Please try to isolate the problem, and put it in a plnkr.

Narretz avatar Oct 08 '14 13:10 Narretz

This Plunker calls 2 concurrent requests, one to the Instagram API and the other to the Flickr API. The problem don't occur is this Plunker. I really don't know how to deal with this trouble. This is REALLY annoying.

mateusleon avatar Oct 08 '14 15:10 mateusleon

without a way to reproduce the issue, there is not a lot that can be done. I am going to close this, but feel free to reopen it if you have some extra information

lgalfaso avatar Oct 09 '14 19:10 lgalfaso

I don't think we should close this, it is competing. Just needs a reproduction

caitp avatar Oct 09 '14 22:10 caitp

Compelling even =)

caitp avatar Oct 09 '14 22:10 caitp

@caitp I need to admit that I'm perplexed with this issue that I'm dealing with.

The palliative solution was the enqueue of all requests that the application use to guarantee the correct association of the responses.

It can be an API problem, but I don't know how far the angular can interfere on the process.

The attempt that I've submitted above uses two large scale public APIs that are necessarily simple to work with because the magnitude of the services.

On my application we will need to attend this feature too, for sure, so all elements that can be managed to steer us to the correct service will be wellcome.

The current API needs one custom header in the request, to contextualize the request according to its needs. Depending on the data that we are looking for, we send path params AND/OR query params AND/OR data payloads.

In other applications that we are working with, none of them needs a custom header on its architecture and, because of this or not, they work fine. I've requested already revision on the backend but, from my side, I'm looking for some enlightenment.

About the places at the "angularjs codes" that the requests take place, they vary between routing and angular-ui-bootstrap modal openings, through save/update controller methods that use services with the functionality $http. It means: in all places that we need to use two or more concurrent requests we faced this problem.

If this info contributes with a test case, I'll be honored to be instructed about how to recriate this situation, remembering that I'll no be able to open the API for these tests.

Thanks, again, for the attention.

mateusleon avatar Oct 10 '14 01:10 mateusleon

What version of angular are you using? Can you chekc if the problem goes away when you use older versions?

Narretz avatar Oct 10 '14 09:10 Narretz

@Narretz I'm using the version 1.3.0-rc.4. Previously, I was using the 1.2.16, when I've started developing the application, and the problem ocurred with it too.

mateusleon avatar Oct 10 '14 12:10 mateusleon

Hello there,

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.

mateusleon avatar Jan 20 '15 22:01 mateusleon