Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carry out Javascript/jQuery functions in a sequence

I want to make some wine. And my function does:

function wine(){
    growGrapes();
    process(grapes);
    makeWine();
    bottle();
}

However, Since my functions often consist of $.ajax() request, some other functions get carried out first. I have used the success tool, but it helps for one ajax request only.

success:function(result){
    //Some Code         
}

What I actually want is a sequence. Literally, grapes get processed before growing them. What is a easiest approach?

like image 401
tika Avatar asked Jan 10 '23 05:01

tika


1 Answers

jQuery Deferred Objects & Promises are the way to go. http://api.jquery.com/category/deferred-object/

They supports running multiple tasks in parallel or series using $.when(PassArrayOfPromisesToRunInParallel) to run processes in parallel and promise.then() to run items sequentially.

like image 172
Gone Coding Avatar answered Jan 17 '23 14:01

Gone Coding