javascript - How to 'chain' JS function after the first is fully done -


i have functions should run 1 after other, such:

function cuttomatoesalone(kg){     // slice stuff } function cooktomatoes(minutes){     // boil stuff } 

i call them such:

cuttomatoesalone(15) // 15kg, need 3 hours! cooktomatoes(10); // need 10 minutes 

but cooktomatoes(10) finish before cuttomatoesalone(15).

how run cuttomatoesalone(15) first , when finished, run cooktomatoes(10) ?


edit: cuttomatoesalone() load external json. cooktomatoes(10) work on it.

learn promises , deferred objects. every ajax function in jquery returns promise, can chain function calls.

for example:

function cuttomatoesalone(kg) {     return $.getjson(...); // return promise provided $.getjson }  // called cuttomatoesalone(15).then(function() { // attach callback     cooktomatoes(10); }); 

in case of ajax call, promise resolved once response retrieved.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -