0

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

  1. Receive the request to execute the function
  2. Add the params to a queue
  3. Call the queuingFunction with the queue
  4. Run the queuingFunction with the queue[0]
  5. On complete of queuingFunction delete the queue[0]
  6. 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.

2 Answers 2

3

It sounds from your comments like you want to be able to just make some function call and have it's operation automatically sequenced with the ones that came before. If so, you will need some sort of queue object that can accumulate the operations underway and pending.

Promises already are a sort of queue, so if you can start with a promise and just add a new promise onto the end of the prior operation each time you make your function call, it can sequence them. Here's one way to do that:

// initialize queue with a resolved promise
var queue = $q();

function queuingFunction(name){
    // chain this operation onto whatever operation was previously queued
    queue = queue.then(function() {
        var deferred = $q.defer()

        $timeout(function(){
            console.log("my name is ",name);
            deferred.resolve(true);
        },3000)

        return deferred.promise;
    });
}

Then, you can just call:

queuingFunction("Jack"); 
queuingFunction("max"); 
queuingFunction("Ray");

And, the three operations will be sequential.

Here's a working demo using ES6 standard promises: http://jsfiddle.net/jfriend00/adq3L6zt/

Sign up to request clarification or add additional context in comments.

2 Comments

Yes this is exactly what i was looking for as per your fiddle. Im just trying to do it in angular using the $q as i have used this primary in my project.
@Gaurav_soni - I don't use $q myself (much prefer standard ES6 promises), but I put my guess as the code in my answer. The only thing I'm not sure of is $q() for manufacturing a resolved promise to initialize the queue. If that doesn't work, then you can do it a longer way by creating a deferred and then resolving it and assign to queue.
0

Using @jfriend00 approach I did the following using angular $q if anyone interested

var queue = $q(function(resolve,reject){resolve()});

        service.queuingFunction = function(name){
          // perform some asynchronous operation, resolve or reject the promise when appropriate.
          queue = queue.then(function() {
                var deferred = $q.defer()

                $timeout(function(){
                    console.log("my name is ",name);
                    deferred.resolve(true);
                },3000)

                return deferred.promise;
            });
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.