2

So consider the following scenario:

i want to check the stock-availability of a product on multiple occasions in my ecommerce-script.

What i did was

var checkStock = function(id) {
$.ajax({
    type: "POST",
    dataType:'json',
    url: "class/updateCart.php",
    data: { productID: id, action: 'checkStock' }
}).done(function(data) {
    return parseInt(data.stock);
}).fail(function(data) {
    return 'AJAX FAILED';
});
}

So now i wanted to use it like this:

if(checkStock(productID) == 0) {
    // do something (A)
}

this obviously did not work so on my research i came across this:

jQuery: Return data after ajax call success & How do I return the response from an asynchronous call?

with really great explanations for deferred objects / promises. But somehow i'm not able to see the difference between

var checkStock = function(id) {
    $.ajax({
        type: "POST",
        dataType:'json',
        url: "class/updateCart.php",
        data: { productID: id, action: 'checkStock' }
    }).done(function(data) {
        // DO SOMETHING (A)
    }).fail(function(data) {
        // DO SOMETHING (B)
});
}

and (the deferred object way)

function checkStock() {
    return $.ajax(...);
}
checkStock().done(function(result) {
    // DO SOMETHING (A)
}).fail(function() {
    // DO SOMETHING (B)
});

both options only allow me to DO SOMETHING (A) everytime the function is a success. But i want to check the stock availability and do different stuff with the result.

For example somewhere in my code i do this:

if(checkStock(productID) == 0) {
    // do something (A)
}

and somewhere else i do something completely different

if(checkStock(productID) > 5) {
    // do something completely different
}

So my questions are:

  1. What's the difference between deferred objects and the simple .fail/.success-callbacks of the ajax-call?
  2. Is it possible to do what i want without making the ajax call synchronous? I want to use the same ajax-Call and use the result differently depending on the context where i use the ajax-call.
Community
  • 1
  • 1
ProblemsOfSumit
  • 19,543
  • 9
  • 50
  • 61
  • You really can't do that. There is no way to return the result from an ajax call the way you want to do it, unless you do it synchronously, which you don't want to do. You have to use a callback or return a promise where you again would have a callback function, so using an extra promise doesn't solve the issue as the ajax call already returns a promise. – adeneo Dec 19 '13 at 14:40

1 Answers1

1

The point of using promises is to allow you to separate callbacks from the original operation (by returning a promise), and to chain multiple asynchronous operations.

You need to move your code into the promise callback:

checkStock(productId)
    .then(function(result) {
        if (result > 5) {
            ...
        }
    });

You can add different promise callbacks each time you call the function.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • yes i realize that this would "solve" it but it's not what i want. My code needs a lot of context-variables. I don't want to pass all those to this ajax-call. I asked this question to avoid this because somehow deferred objects are sold as the solution to this (like syncronous code, only better). And i still fail to see how to do that. – ProblemsOfSumit Dec 19 '13 at 14:35
  • You don't need to. Just put the promise callback in your calling function. It can access all of your variables via closure. – SLaks Dec 19 '13 at 14:38
  • Holy smokes i get it! All my questions are answered. Thank you very much. I misunderstood how the code is used. I thought this was defining the callbacks but instead this is defining and executing every time. Love it! – ProblemsOfSumit Dec 19 '13 at 16:02