0

I have this code.

function doReq() {
    return $.get('http://localhost:5000/fun/test.php').done(
        function(data) {
            var x = data + "uui";

    }).done(
        function(data) {
            alert(x);
        }
    );
}

doReq();

x is undefined, because it is out of scope.

Is there a way to pass additional variables, apart from just data down the pipeline?

user350325
  • 1,355
  • 3
  • 15
  • 24
  • Just as you're alluding in your question, take a look at the deferred() object from jquery: http://api.jquery.com/category/deferred-object/ – Jose Jun 24 '13 at 16:57
  • Duplicate of http://stackoverflow.com/questions/8357188/passing-data-between-deferred-functions? – dav Jun 24 '13 at 16:59

1 Answers1

1

Not with the same deferred object, as you have no control over the arguments passed to resolveWith() in this case. However, the callbacks will run in the order defined, so you could define var x at the top of the function, and it would be accessible in both callbacks' scope (and those functions will manipulate the variable sequentially).

Otherwise, you would need a second deferred, and you could resolve it with whatever arguments you want.

landons
  • 9,502
  • 3
  • 33
  • 46