Reputation: 717
How can I use myValue in this deferred function so that myQuery is defined?
$.getJSON('lorem.json', function (data) {
var myKey = $("input").val();
var myValue = data[myKey];
}).then(function () {
var myQuery = "http://example.com?q=" + myValue ;
$.getJSON(myQuery, function (info) {
console.log(info);
...
Upvotes: 2
Views: 946
Reputation: 1184
You can use the parent context
var myValue = null;
$.getJSON('lorem.json', function (data) {
var myKey = $("input").val();
myValue = data[myKey];
}).then(function () {
var myQuery = "http://example.com?q=" + myValue ;
$.getJSON(myQuery, function (info) {
console.log(info);
Upvotes: 3