jquery - Asynchronous execution of functions in javascript -


i want call 2 function function a() , function b() parallely . these function independent each other. , lets time require execute these 2 functions not fixed . function a() take more time function b() , wise versa. function c() should execute when both (a , b ) functions completed . how should using jquery's deferred object?

to achieve can make a() , b() functions return deferred objects resolve() once logic has completed. can run c() once both previous functions have completed. try this:

function a() {    var adef = $.deferred();    settimeout(function() {      adef.resolve('a done');    }, 1000);    return adef;  }    function b() {    var bdef = $.deferred();    settimeout(function() {      bdef.resolve('b done');    }, 3000);    return bdef;  }    function c() {    console.log('all done!')  }    console.log('running...');  $.when(a(), b()).done(function(a, b) {    console.log(a);    console.log(b);    c();  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -