最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Execute a function after $.ajax success without putting it in a callback directly - Stack Overflow

programmeradmin1浏览0评论

I have this function where many parts of my code call it.

function test() {
  $.ajax({
    url : url,
    type : 'GET',
    success : {
      verifyID();
      verifyName();
      verifyBlah();
    }
  });
}

and I have this other function:

addProductCart(productID);

Before I call addProductCart(), I need to call test function, but, other processes call test function.

I'd like to do this:

test() ---> if test ok (success) ----> addProductCart()

But I can't set my function (addProductCart) into success test function because, like I said, many other processes call test function.

How can I do this?

I have this function where many parts of my code call it.

function test() {
  $.ajax({
    url : url,
    type : 'GET',
    success : {
      verifyID();
      verifyName();
      verifyBlah();
    }
  });
}

and I have this other function:

addProductCart(productID);

Before I call addProductCart(), I need to call test function, but, other processes call test function.

I'd like to do this:

test() ---> if test ok (success) ----> addProductCart()

But I can't set my function (addProductCart) into success test function because, like I said, many other processes call test function.

How can I do this?

Share Improve this question edited Apr 12, 2017 at 4:16 Shaunak 18.1k5 gold badges56 silver badges86 bronze badges asked Apr 12, 2017 at 3:47 Vinicius LimaVinicius Lima 5447 silver badges20 bronze badges 4
  • probably need to see the other code as well, to see what the test function is doing, and what the ones calling them is using test for – A. L Commented Apr 12, 2017 at 3:52
  • 2 may be this can help you stackoverflow./questions/14220321/… – Thanh Nguyen Commented Apr 12, 2017 at 3:53
  • What about making the test function return a boolean and just put an if statement in the success block? Not sure if I understand correctly the question. – rpabood Commented Apr 12, 2017 at 3:54
  • The promises solution shown below is probably best, but you could also modify test() to accept optional arguments that specify additional callbacks to be run. – nnnnnn Commented Apr 12, 2017 at 4:28
Add a ment  | 

1 Answer 1

Reset to default 7

Use Promises!

Return a promise from the test function like so:

function test() {
  return $.ajax({ // <----- Notice the return statement here
    url : url,
    type : 'GET',
    success : {
      verifyID();
      verifyName();
      verifyBlah();
    }
  });
}

When you need to use this function to test something and execute another piece of code when this passes you can do :

test().then(function(data, textStatus){
  //do thing 1
  addProductCart()
  // you can use data or textStatus returned by your ajax function here too!
});

test(someParam).then(function(data, textStatus){ // <--- You can even pass parameters to test function like here, and make it do different ajax call based on your param
  //do thing 2
});

For more details on how this works see the jQuery docs for $.ajax. function

Here's a great tutorial on concept of JavaScript Promises to get you started if you are unfamiliar with them.

发布评论

评论列表(0)

  1. 暂无评论