w
Posts: 0
Joined: Tue Mar 26, 2013 12:30 pm

How can i execute a service multiple times, but one after the other? So a so-called waterfall effect?

How can i execute a service multiple times, but one after the other? So a so-called waterfall effect?

For example:

for (var i=0; it get it how to make it work with Appery services, because you have no custom callback functions or explicit ajax calls (most of the examples of deferred objects on internet use this), but just the service.execute({}).

Can you give an example please on how to get this to work for the base case of 1 service that has to execute one after the other, like in the for-loop above?

w
Posts: 0
Joined: Tue Mar 26, 2013 12:30 pm

How can i execute a service multiple times, but one after the other? So a so-called waterfall effect?

The question to use deferred objects with Appery service.execute is: does service.execute return a promise object?

From the deferred object jquery documentation:

precode
$.when( doAjax(), doMoreAjax() )
.done(function() {
console.log( 'I fire once BOTH ajax requests have completed!' );
})

The reason this works is because all of jQuery's AJAX methods now return an object containing a "promise", which is used to track the asynchronous request. The promise is a read-only view into the result of the task. Deferreds look for the presence of a promise() method to determine whether an object is observable or not. The $.when() waits for all its AJAX requests to execute, and once they do, the callbacks attached to the $.when() via .done() and .fail() will fire as appropriate (depending on task's success or failure state). The callbacks fire in the order they were assigned.
/code/pre

And if not, how do you suggest to use deferred objects and when() done() etc with Appery's service.execute({})?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How can i execute a service multiple times, but one after the other? So a so-called waterfall effect?

Hello! No, service.execute doesn't return a promise object. You have to use complete servie event to run services one after the other.

w
Posts: 0
Joined: Tue Mar 26, 2013 12:30 pm

How can i execute a service multiple times, but one after the other? So a so-called waterfall effect?

Hi Marina,

How do you suggest using it with for example this:

precode
for(var i=0; i<=10; i++){
localStorage&#46;setItem("counter", i);
service&#46;execute({});
}

In the success event of the service:

alert("Service " + localStorage&#46;getItem("counter") + " was just executed and returned response!");
/code/pre

If I don't use a deferred object somewhere, the for-loop will run quickly one after the other, and localStorage Item "counter" will have a value of 10 for every success event of the service.execute, because there is a delay between the service.execute statement and the actual success callback due to the underlying ajax call.

So the for-loop will have ended (and localStorage item 'counter' will have its final value of 10) before a success callback is called.

w
Posts: 0
Joined: Tue Mar 26, 2013 12:30 pm

How can i execute a service multiple times, but one after the other? So a so-called waterfall effect?

Or imagine that the response of the 1st service.execute is the request input for the 2nd service.execute etcetera...

It's just a matter of being able to queue service.execute statements and start another when the previous one has completed.

I guess a lot of Appery users will need this functionality?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How can i execute a service multiple times, but one after the other? So a so-called waterfall effect?

1) Describe i as a global variable - create JS asset with code:
codevar i = 0;/code (or you can use local storage to store i)
2) call service on button click;
3) on service success event run the following code:
codeif (i < 10) {
i++;
service&#46;execute({});
}/code

Return to “Issues”