AngularJS multiple Controllers with one Service: Asynchronous errors

I am getting some asynchronous errors when executing a function on my controllers near-simultaneously. Each controller takes some data, and calls a method in a service for testing. The service returns a promise to the controller, manipulates the data passed in, then resolves the promise. The code outline for the service looks like so:

//Test function which takes a group, and returns a promise with the result
this.Test = function(resultsLocation, testList, testFunction) {
//promise we are returning
var deferred = $q.defer();
var i = 0;

"Testing" is called on a button press in the html. The problem is if controller1 calls "Testing", then controller2 calls "Testing", the promise will never be resolved in controller1. Worse still, some of the test results are pushed into controller 2's results.

Perhaps I'm missing something, but I could've sworn I read somewhere that a service will be it's own instance when a controller has it.

Pressing "Test" individually is fine, but if you press "Test" while the other controller is testing, you'll get odd behavior such as values being mixed up, and the first controller will never finish testing.

So just to clarify, declaring a function as a var puts it on the global scope even when declared inside a function?

The error was setting a value to an undeclared variable.

Assigning a value to an undeclared variable implicitly creates it as a global variable (it becomes a property of the global object) when the assignment is executed. The differences between declared and undeclared variables are:

Declared variables are constrained in the execution context in which they are declared. Undeclared variables are always global.

Declared variables are created before any code is executed. Undeclared variables do not exist until the code assigning to them is executed.

Declared variables are a non-configurable property of their execution context (function or global). Undeclared variables are configurable (e.g. can be deleted).

Because of these three differences, failure to declare variables will very likely lead to unexpected results. Thus it is recommended to always declare variables, regardless of whether they are in a function or global scope. And in ECMAScript 5 strict mode, assigning to an undeclared variable throws an error.1

In this case, when the service.Test function was called the second time, the global value TestCallBack was being replaced by a reference to the anonymous function in the second instantiation of the service.Test function. The anonymous TestCallBack function refers to a closure2 of the service.Test function. A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. So functions scheduled by the first instantiation were getting their i and resultsLocation switched to the closure of the second instantiation of the service.Test function.