javascript - how to execute a function after finishing the first one -


i have input :

<button type="button" title="test" class="button btn-cart" onclick="productaddtocartform.submit(this); redirecttocart();">ok</button> 

i want execute redirecttocart() after finishing first 1 productaddtocartform.submit(this) without moving first on, mean productaddtocartform.submit(this) should stay in onclick button .

function redirecttocart() {     window.location.replace("http://stackoverflow.com"); } 

i'm making few assumptions here, principle should work.

primarily, i'm assuming productaddtocardform.submit(this) triggering ajax request, either natively or using jquery. in either case, need use promises handle this.

there's comprehensive guide here, basics follows:

  1. return promise first function
  2. run second function when first function resolves successfully

that might this:

function myfirstfunction(packet) {   return new promise(function(resolve, reject) {     resolve(productaddtocartform.submit(packet))   });  function doboththings(packet) {   myfirstfunction().then(redirecttocart()) }; 

i've put quick fiddle demonstrates better.

nb: simplest use case promises, powerful tool. full specs here.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -