Difference between Sequential and Parallel executing in $q

2.3k Views Asked by At

In both of at the time of execution of an asynchronic operation only.


  • But how the $q handle for this is a Sequential call or Parallel call on runtime?
  • and let me brief explanation about the Difference between Sequential and Parallel executing in angular $q
2

There are 2 best solutions below

0
sonu singhal On BEST ANSWER

Parallel Execution is something in which it doesn't wait for the previous process to be done,and Sequential is something in which process are executed one after another.

$q service is used for asynchronous call (parallel execution | promise handling),by default it does parallel execution,it does not have support for sequential execution. If you want a sequential execution you have to handle it manually, wheich means after getting response from one call you make another call.

var promise;
promise.then(fun(){
   var promise;
   promise.then(fun(){
   })
})
0
georgeawg On

To execute promises in parallel:

var promise1 = promiseAPI(params1);
var promise2 = promiseAPI(params2);

var promise1and2 = $q.all([promise1, promise2]);

To execute promises sequentually, return the next promise to the first promise success handler:

var promise1 = promiseAPI(params1);

var promise1then2 = promise1.then(function() {
    var promise2 = promiseAPI(params2);
    //return to chain
    return promise2;
});

Because calling the .then method of a promise returns a new derived promise, it is easily possible to create a chain of promises. It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain.

-- AngularJS $q Service API Reference -- Chaining Promises.