I have a function like this
function queuingFunction(name){
var deferred = $q.defer()
$timeout(function(){
console.log("my name is ",name);
deferred.resolve(true);
},3000)
return deferred.promise;
}
i am calling the function like this
communicatorBaseService.queuingFunction("Jack"); communicatorBaseService.queuingFunction("max"); communicatorBaseService.queuingFunction("Ray");
In the console all the 3 results are displayed after 3 second.
What i need is , at the 3rd second Jack shows in console, after again 3 seconds max is shown then again after 3 second Ray is shown.
If i call queuingFunction in between the execution it should get added to a execution queue.
What i was thinking to do was
- Receive the request to execute the function
- Add the params to a queue
- Call the queuingFunction with the queue
- Run the queuingFunction with the queue[0]
- On complete of queuingFunction delete the queue[0]
- Check if anything is left at position queue[0] re-run the function with the current queue.
Pretty sure this is not the best way, what can be a good way to do this. I am using Angular thus $q is there in code. I don't want to use jQuery.