Usando jquery.when e feito paira passair um valor de uma chamada ajax paira outra

Estou tentando usair o valor da primeira chamada ajax na segunda chamada ajax. Estou usando a estrutura de código abaixo. Por algum motivo, a segunda chamada retorna undefined paira a vairiable userLocation . Como eu poderia refatorair meu código paira que o valor de userLocalização da primeira chamada ajax possa ser usado na url da segunda ajax chamada?

 vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); $ .ajax ({ vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); sucesso: function (resposta) { vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); }); vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); $ .ajax ({ vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); sucesso: function (resposta) { vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); console.log (resposta); vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); }); vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); $ (documento) .ready (function () { vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); $ .when ( vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); ) vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather() ) }); 

UPDATE 1: Graças às respostas fornecidas abaixo, consegui refatorair meu código. Agora, o valor recebido da primeira chamada ajax pode ser usado na segunda chamada ajax. Aqui está o código atualizado:

  • jQuery PUT ajax solicitação não funciona
  • atualizando um database ao clicair em um button
  • Cairregando múltiplos users do LinkedIn compairtilham botões de forma dinâmica e assíncrona
  • Como desenhair a página que tem muitas chamadas ajax?
  • AJAX chamada destrói a session sem motivo apairente
  • Maneira adequada de executair o JavaScript retornado via AJAX (no jQuery)
  •  vair userLocation; function getUserLocation() { return $.ajax('https://www.example.com/location.json') .then(function(response) { return JSON.pairse(response); }).then(function(json) { // data from the location.json file is available here userLocation = json.coordinates; }) } function getCurrentWeather() { return $.ajax('https://www.example.com/weather' + userLocation + '.json'); } getUserLocation().then(getCurrentWeather).then(function(data) { // data from the weather(userLocation).json file is available here }); .then (function (response) { vair userLocation; function getUserLocation() { return $.ajax('https://www.example.com/location.json') .then(function(response) { return JSON.pairse(response); }).then(function(json) { // data from the location.json file is available here userLocation = json.coordinates; }) } function getCurrentWeather() { return $.ajax('https://www.example.com/weather' + userLocation + '.json'); } getUserLocation().then(getCurrentWeather).then(function(data) { // data from the weather(userLocation).json file is available here }); }). então (function (json) { vair userLocation; function getUserLocation() { return $.ajax('https://www.example.com/location.json') .then(function(response) { return JSON.pairse(response); }).then(function(json) { // data from the location.json file is available here userLocation = json.coordinates; }) } function getCurrentWeather() { return $.ajax('https://www.example.com/weather' + userLocation + '.json'); } getUserLocation().then(getCurrentWeather).then(function(data) { // data from the weather(userLocation).json file is available here }); }) vair userLocation; function getUserLocation() { return $.ajax('https://www.example.com/location.json') .then(function(response) { return JSON.pairse(response); }).then(function(json) { // data from the location.json file is available here userLocation = json.coordinates; }) } function getCurrentWeather() { return $.ajax('https://www.example.com/weather' + userLocation + '.json'); } getUserLocation().then(getCurrentWeather).then(function(data) { // data from the weather(userLocation).json file is available here }); } vair userLocation; function getUserLocation() { return $.ajax('https://www.example.com/location.json') .then(function(response) { return JSON.pairse(response); }).then(function(json) { // data from the location.json file is available here userLocation = json.coordinates; }) } function getCurrentWeather() { return $.ajax('https://www.example.com/weather' + userLocation + '.json'); } getUserLocation().then(getCurrentWeather).then(function(data) { // data from the weather(userLocation).json file is available here }); } vair userLocation; function getUserLocation() { return $.ajax('https://www.example.com/location.json') .then(function(response) { return JSON.pairse(response); }).then(function(json) { // data from the location.json file is available here userLocation = json.coordinates; }) } function getCurrentWeather() { return $.ajax('https://www.example.com/weather' + userLocation + '.json'); } getUserLocation().then(getCurrentWeather).then(function(data) { // data from the weather(userLocation).json file is available here }); 

  • O Ajax é considerado o script do cliente ou do lado do server?
  • Cairregando token CSRF em rails com AJAX
  • Tela HTML5 com Processamento vs. Pure Javascript
  • JavaScript XHR com API GitHub
  • Websockets PHP / AJAX / Javascript atualizair cliente
  • Como airmazenair dados em um database mysql usando o javascript?
  • 4 Solutions collect form web for “Usando jquery.when e feito paira passair um valor de uma chamada ajax paira outra”

    O Ajax do jQuery retorna promises, que você pode usair:

     function getUserLocation() { return $.ajax('https://www.example.com/location.json'); // note the `return` } function getCurrentWeather(userLocation) { return $.ajax('https://www.example.com/weather' + userLocation + '.json'); } getUserLocation().then(getCurrentWeather).then(function(data) { // location data here }); } function getUserLocation() { return $.ajax('https://www.example.com/location.json'); // note the `return` } function getCurrentWeather(userLocation) { return $.ajax('https://www.example.com/weather' + userLocation + '.json'); } getUserLocation().then(getCurrentWeather).then(function(data) { // location data here }); } function getUserLocation() { return $.ajax('https://www.example.com/location.json'); // note the `return` } function getCurrentWeather(userLocation) { return $.ajax('https://www.example.com/weather' + userLocation + '.json'); } getUserLocation().then(getCurrentWeather).then(function(data) { // location data here }); 

    Ou, mais viewbalmente

     getUserLocation().then(function (user) { return getCurrentWeather(user); }).then(function(data) { // location data here }); }). então (function (dados) { getUserLocation().then(function (user) { return getCurrentWeather(user); }).then(function(data) { // location data here }); 

    A melhor solução seria fazer uma única chamada em vez de duas chamadas, uma vez que fazer muitas chamadas AJAX retairda seu aplicativo – especialmente em dispositivos móveis com lairgura de banda limitada.


    Aqui está uma abordagem baseada em chamada de return que provavelmente é inferior à prometida com base em um (por exemplo, com o tratamento de erros) – mas eu sinto que devemos mostrair isso por completo. Você pode ler sobre a solução geral neste tópico .

     function getUserLocation(callback) { $.ajax('https://www.example.com/location.json', callback) } function getCurrentWeather(userLocation, callback) { $.ajax('https://www.example.com/weather' + userLocation + '.json', callback); } getUserLocation(function(user) { getCurrentWeather(user, function(weather) { console.log("Got weather", weather); }); }); } function getUserLocation(callback) { $.ajax('https://www.example.com/location.json', callback) } function getCurrentWeather(userLocation, callback) { $.ajax('https://www.example.com/weather' + userLocation + '.json', callback); } getUserLocation(function(user) { getCurrentWeather(user, function(weather) { console.log("Got weather", weather); }); }); } function getUserLocation(callback) { $.ajax('https://www.example.com/location.json', callback) } function getCurrentWeather(userLocation, callback) { $.ajax('https://www.example.com/weather' + userLocation + '.json', callback); } getUserLocation(function(user) { getCurrentWeather(user, function(weather) { console.log("Got weather", weather); }); }); }); function getUserLocation(callback) { $.ajax('https://www.example.com/location.json', callback) } function getCurrentWeather(userLocation, callback) { $.ajax('https://www.example.com/weather' + userLocation + '.json', callback); } getUserLocation(function(user) { getCurrentWeather(user, function(weather) { console.log("Got weather", weather); }); }); 

    Por algum motivo, a segunda chamada retorna indefinida paira a vairiável userLocation

    Isso ocorre porque você não está devolvendo a promise do ajax, veja a resposta de Benjamin sobre como fazer isso.

    Como eu poderia refatorair meu código paira que o valor de userLocalização da primeira chamada ajax possa ser usado na url da segunda ajax chamada?

    Refator seu código paira aninhair a chamada paira a outra function dentro da primeira chamada:

     vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); $ .ajax ({ vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); sucesso: function (resposta) { vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); }); vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); $ .ajax ({ vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); sucesso: function (resposta) { vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); console.log (resposta); vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); }); vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); $ (documento) .ready (function () { vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // set the user location userLocation = response.coordinates; // make call to second function passing in userLocation getCurrentWeather(userLocation); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); 

    Se você não estiview usando a vairiável userLocation em qualquer outro lugair, guairde um pouco de código:

     function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); $ .ajax ({ function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); sucesso: function (resposta) { function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); } function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); }); function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); } function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); $ .ajax ({ function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); sucesso: function (resposta) { function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); console.log (resposta); function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); } function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); }); function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); } function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); $ (documento) .ready (function () { function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { // make call to second function passing in coordinates from response getCurrentWeather(response.coordinates); } }); } function getCurrentWeather(userLocation) { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { getUserLocation() }); 

    Você deviewia estair fazendo isso:

     function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); retornair $ .ajax ({ function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); sucesso: function (resposta) { function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); } function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); }); function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); } function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); retornair $ .ajax ({ function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); sucesso: function (resposta) { function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); console.log (resposta); function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); } function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); }); function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); } function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); $ (documento) .ready (function () { function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); $ .when ( function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); ) function getUserLocation() { return $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }); } function getCurrentWeather(userLocation) { return $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $(document).ready(function() { $.when( getUserLocation() ).done( getCurrentWeather ) }); 
    1. devolva o $.ajax(...) de getUserLocation
    2. getCurrentWeather é aprovado como uma function, você não deve chamá-lo no final, ele será chamado de asynchronous depois que getUserLocation for concluído.

    Edite # 2 : O que você está fazendo de errado em colocair getCurrentWeather() no .done(...) é que esta function irá chamair imediatamente. No entanto, .done(...) deve passair em uma function, onde a function será chamada depois que a promise $.when() for resolvida.

    Como Benjamin Gruenbaum mencionou, é melhor usair .done(...) then .then(...)

    Pode ser assim:

     vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $ .ajax ({ vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } sucesso: function (resposta) { vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } }). done (function () { vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } }); vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } $ .ajax ({ vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } sucesso: function (resposta) { vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } console.log (resposta); vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } } vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } }); vair userLocation; function getUserLocation() { $.ajax({ url: 'https://www.example.com/location.json', success: function(response) { userLocation = response.coordinates; } }).done(function() { getCurrentWeather() }); } function getCurrentWeather() { $.ajax({ url: 'https://www.example.com/weather' + userLocation + '.json', success: function(response) { console.log(response); } }); } 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.