1

Have a look at the following examples:

var data = "abc";   

$.post(ajaxurl,data,function(results) {
    console.log(data);
    console.log(results)
} ,'JSON');

data = "ghi";

Result:

data: "ghi"

results: "results"

Passing data:

var data = "abc";   

$.post(ajaxurl,data,function(results) {
    console.log(data);
    console.log(results)
} (data) ,'JSON');

data = "ghi";

Result:

data: "abc"

results: "abc"

However what I want to achieve is to pass the local variable data but recieve the passed variables from the callback anyway.

Desired output:

data: "abc"

results: "results"

In PHP this would be something like:

function abc($result) use ($data) {
    //...
}

How can I achieve this behaviour?

Edit - Clarification: I want to pass the current state of a local variable to an aynchronous request callback with arguments.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • You want the value of the variable `results` to be the string 'results' but you never set it to that in the code? You also *seem* to want your AJAX callback to have access to the original value of `data` while you go off on modify `data` while the async op is completing? Am I getting that correctly? – Jared Smith Nov 28 '17 at 13:55
  • @JaredSmith Yes this is almost correct. Do not care about `results` - passing of the callback result is automatically handled by jQuery. My problem is how can I pass in the current state of `data` such that it does not override results if passed in like in the second example. – Blackbam Nov 28 '17 at 13:57
  • Well guys I really do not understand why to close this the problem statement is really clear. I want to pass the current state of a local variable to an aynchronous request callback with arguments. – Blackbam Nov 28 '17 at 14:01
  • 1
    @Blackbam your edit made it clear, and I retracted my close vote and posted an answer. – Jared Smith Nov 28 '17 at 14:06
  • Probably related [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – charlietfl Nov 28 '17 at 14:13

2 Answers2

3

Based on the conversation in the comments, you want this:

var data = "data";
$.post(ajaxurl,data,(function(data) { // shadows outer data
  return function(results) {
    console.log(data); // logs "data" not "foo"
    console.log(results); // logs the AJAX results
  };
})(data) ,'JSON');
data = "foo";

This will log "data" to the console rather than "foo" even though the async op completes later.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
2

You can use an IIFE to create a closure around the whole request. Assumes data is a primitive value. If it is array or object you either need to copy it or use promise callback to change it since arrays and objects are passed by reference not value

var data = "abc"; 

(function(data){    

    $.post(ajaxurl,data,function(results) {
     console.log(data); // abc
     console.log(results)
     } ,'JSON');

})(data);

data = "ghi";
charlietfl
  • 170,828
  • 13
  • 121
  • 150