-3

I know this has been asked before. I have read the answer to the question here several times: How do I return the response from an asynchronous call?. I realize that I need to create a callback function to return the result. I'm not sure how to do that as explained here- How do I return the response from an asynchronous call? - but with parameters passed into the function as well (optionSelectionArray).

function simpleWithAttrPrice(optionSelectionArray){
    var product_id= <?=$product_id ?>;


        $j.ajax({
            type: "POST",
            url: "/ajax_calls/childrenToJs.php",
            data: { 'productID': product_id, 'optionSelectionArray' : optionSelectionArray} 
            }).done(function(data) {
                return price;
            }); 


}
Community
  • 1
  • 1
CaitlinHavener
  • 1,408
  • 3
  • 24
  • 53
  • Did you read the answer to that question? – SLaks Jun 10 '13 at 18:34
  • "I have read the answer to the question here several times" – CaitlinHavener Jun 10 '13 at 18:34
  • So it's said there that you should use callback function. – dfsq Jun 10 '13 at 18:35
  • The example he starts with is var result = foo(); // code that depends on `result` becomes foo(function(result) { // code that depends on `result` }); – CaitlinHavener Jun 10 '13 at 18:35
  • But I have to send a value into the function as well. How do you do both? – CaitlinHavener Jun 10 '13 at 18:36
  • 3
    I think you're just missing the point... The conclusion is that returning values from asynchronous calls doesn't work because you don't know at what point that's going to be assigned, since the call is **asynchronous**, it'll return when it can, and that's probably too late. So you need to do whatever you want to do with the result _inside_ the callback. – elclanrs Jun 10 '13 at 18:39
  • 1
    So you pass in multiple arguments to the function, e.g., `function simpleWithAttrPrice(optionSelectionArray, callback){ ...`. That's it -- [the question you link](http://stackoverflow.com/q/14220321/710446) is perfectly applicable here; you simply need to pass in the callback function as the second argument and pass `callback` into `done` (or use it as the `success` function to `$.ajax`). – apsillers Jun 10 '13 at 18:40
  • 2
    You can't return anything from a function that is asynchronous.you can add async option to false. – Rakesh Singh Jun 10 '13 at 18:41
  • @RakeshSingh Sure you can, you can return the result of the `$.ajax()` call (which is a deferred object) and work with that. – Ian Jun 10 '13 at 18:42
  • I can't quite grasp what exists in your question that is not addressed in the linked duplicate (aside from the number of arguments to `simpleWithAttrPrice` and the use of `.done` instead of `success:`). Can you please *edit your question* to articulate *precisely* what you don't understand? (Adding code clarification in the comments is quite messy.) Providing such a clarification may save your question from getting closed and may make it valuable to others in the future, depending on your exact misunderstanding. – apsillers Jun 10 '13 at 18:48
  • Okay I edited it. I'm working on trying the below answer. – CaitlinHavener Jun 10 '13 at 18:53

1 Answers1

3

You should make use of callback function calling it (.done(callback) will invoke callback) instead of returning as you are doing right now:

function simpleWithAttrPrice(optionSelectionArray, callback) {
    var product_id = <?= $product_id ?>;
    $j.ajax({
        type: "POST",
        url: "/ajax_calls/childrenToJs.php",
        data: { 'productID': product_id, 'optionSelectionArray' : optionSelectionArray} 
    }).done(callback);
}


// Usage
simpleWithAttrPrice(options, function(data) {
    // Here you have you response
    console.log(data);
});
dfsq
  • 191,768
  • 25
  • 236
  • 258