Multiple Http Client Requests Not Storing Session Data
Solution 1:
Your problem is the fact that you're executing both calls at the same time. So the order of execution is unknown. What you need to do is call the 2nd after the first has finished. For this to work you will need to add the second http call within the callback of the first.
And to make your code more organised I recommend using functions! Makes it also more readable.
function doBothCalls(){
doFirstCallFunction(function(){
doSecondCallFunction();
}
}
The doFirstCallFunction
then gets a callback function, this callback function you should call after the first one has gotten into the http callback.
Solution 2:
What you need here is called Promises in Javascript.
When you do async calls, they all happen in random order of time , so you cannot do a async call which depends on result of another async call in same execution context(which you are doing in your code)
To overcome this, Javascript has functionality for promises
which in nutshell means:
A Promise object represents a value that may not be available yet, but will be resolved at some point in the future. It allows you to write asynchronous code in a more synchronous fashion. For example, if you use the promise API to make an asynchronous call to a remote web service you will create a Promise object which represents the data that will be returned by the web service in future.
Post a Comment for "Multiple Http Client Requests Not Storing Session Data"