Page 1 of 1

Using $q (or other functions) to sequence function calls

Posted: Thu Mar 10, 2016 7:52 am
by leven.steve

do you have any examples showing how to use $q (promise)?
I want to make 2 or 3 list service calls to fill select lists BEFORE I start an edit. I need to queue them up or at least execute one function that does all of the work either before the main "read" request or withing the "success" function of the read before the success code runs.


Using $q (or other functions) to sequence function calls

Posted: Thu Mar 10, 2016 9:15 pm
by Serhii Kulibaba

Hello Steve,

Please clarify, what app type do you use, jqm or AngularJS? Please use default promices, according to your project type:
jqm: https://api.jquery.com/promise/
AngularJS: https://docs.angularjs.org/api/ng/ser...


Using $q (or other functions) to sequence function calls

Posted: Fri Mar 11, 2016 11:57 am
by leven.steve

Hi Sergiy - it's Angular/Bootstrap
I have already looked at the Angular $q documentation but havent got anything working yet. Just wanted to see if you can provide a very simple example of chaining 2 or 3 functions together please


Using $q (or other functions) to sequence function calls

Posted: Fri Mar 11, 2016 4:37 pm
by Serhii Kulibaba

Please look at the example below:
prevar $q = Apperyio.get("$q");
var $timeout = Apperyio.get("$timeout");

var promises = [];

function createPromise() {
var deferred = $q.defer();
console.log("before 1 function");
$timeout(function() {
console.log("after 1 function");
deferred.resolve();
}, 1000);
return deferred.promise;
}

promises.push(createPromise());

$q.all(promises).then(function(){
console.log("after 2 function");
});/pre

here you can see results in the console:

before 1 function
after 1 function
after 2 function


Using $q (or other functions) to sequence function calls

Posted: Sun Apr 03, 2016 1:18 pm
by Ricardo Amaral

I took few hours to understand this concept. Just to share my sample created based on this video: https://www.youtube.com/watch?v=wA0gZ...

pre

//this is my promise test

var $q = Apperyio.get("$q"); // get the $q function

var defer = $q.defer(); // put function on a variable

function calc(x, y) { // our calc function just to have fun

Code: Select all

 var q = $q.defer(); // create the pormise 

 var preocessCalc = (x + y); //do the calc 

 if (preocessCalc = 0) { //check if calc is positive 

     q.resolve(preocessCalc); // if so, return it and continue 

 } else { 

     q.reject("Stoped! Negative value found:" + preocessCalc); // else return a failure 

 } 

 return q.promise; // return the promise result 

}

defer.promise //this is the promise that will hold to be called

.then(function(receivedValue) { //this is the function for 1st action. receivedValue receive 0 from defer.resolve

Code: Select all

 console.log('1st action: current value is ' + receivedValue); // print the current value 

 return calc(receivedValue, 3); //this return will trigger calc and pass new result to next action 

})

.then(function(receivedValue) { //this is the function for 2nd action. receivedValue received from 1st action return

Code: Select all

 console.log('2nd action: Value received was:' + receivedValue); // print the current value 

 return calc(receivedValue, -6); //TEST HERE - change -6 to 6 to continue the sequence (preocessCalc is negative and will became positive). When negative the sequence stop here.  

})

.then(function(receivedValue) { //this is the function for 3rd action. receivedValue received from 2nd action return

Code: Select all

 console.log('3rd action: Value received was:' + receivedValue); // print the current value 

 return calc(receivedValue, 9); //this return will trigger calc and pass new result to next action if exist 

})

.catch(function(fail){ //this is to handle errors in the sequence. If an error occur (from q.reject - in our case a negative value), the sequence stops.

$scope.fail = console.log(fail);

})

.finally(function(){ //this will be called in case of success or fail of the sequence

Code: Select all

 console.log("Last action, no matter what!"); 

});

defer.resolve(0); // this trigger the promise sequence with initial value of 0 for our calcs /pre

No further help is required for now.