2017-04-15 4 views
1

Ich möchte die a-Funktion ausführen, nachdem eine andere Funktion ausgeführt wurde. Ich habe den Weg gefunden, es zu erreichen, aber mein Problem ist, dass ich die Ausgabe der Funktion getLocation als Argument für die Update-Funktion verwenden möchte, in diesem Fall Station.Funktion ausführen, nachdem andere Funktion ausgeführt wurde

Gibt es eine Möglichkeit, den Code wie folgt auszuführen und eine Variable dem Ausgang der Funktion getLocation zuzuweisen?

function giveAddress(){ 
    $.getJSON('http://localhost:5000/api/stations', function(json) { 
     { 
      $.when($.ajax(getLocation(json))).then(function() { 
       update(station); 
      }); 
     }) 
    } 

Jede Hilfe wird sehr geschätzt.

+0

Was bedeutet 'getLocation' zurückkehren? Ist es ein Versprechen? –

+0

Es gibt einen String zurück, der Name der Station – Reggie

+0

'' '' '' '' '.success'' oder' '.error'' –

Antwort

3

nicht Rückrufe verwenden, wenn Sie Promises haben

function giveAddress(){ 
    return $ 
    .getJSON('http://localhost:5000/api/stations') // first request 
    .then(getLocation) // extract location 
    .then(function(location) { 
     return $.ajax(location) 
     .then(function(){return location}) // second request still return location 
    }) 
    .then(update) 
} 

oder wenn Sie das Ergebnis verwenden, von location

function giveAddress(){ 
    return $ 
    .getJSON('http://localhost:5000/api/stations') // first request 
    .then(getLocation) // extract location 
    .then(function(location) { 
     return $.ajax(location) 
     .then(function(data){return { 
      location: location, 
      data: data} 
     }) // keep location and data 
    }) 
    .then(function(result) { 
     // use result.data 
     update(result.location) 
    }) 
} 
bekommen
+0

Entschuldigung, wenn das eine dumme Frage ist, aber wie würde ich den JSON als Eingabe für die Funktion getLocation in diesem Szenario verwenden? – Reggie

+0

@Reggie automatisch. 'getJSON' gibt ein verspätetes, verspätetes Objekt zurück, das mit dem Objekt (nicht JSON, aber bereits geparst) vom Backend aufgelöst wird. Beachten Sie, dass ich "getLocation" selbst übergebe ".then (getLocation)'. –

0

Sie sind fast da, man muss nur ein data Argument an die Funktion Versprechen then Rückruf hinzufügen und auf Ihre zweite Funktion übergeben es dann:

function giveAddress(){ 
$.getJSON('http://localhost:5000/api/stations', function(json) { 
    { 
     $.when($.ajax(getLocation(json))).then(function (data) { 
      update(data); 
     }); 
    }) 
} 
+0

Wenn ich die Variable" data "auf die Konsole drucke, druckt sie den HTML-Code des Seite, während die Funktion getLocation normalerweise nur den Namen der Station als String ausgibt. Gibt es noch etwas, das ich ändern muss? – Reggie

+0

Vielleicht geben Sie ein falsches Ergebnis in Ihrem Webservice zurück !? –

0

Das einzige, was Sie tun müssen, ist einfach und die Antwort, dass Ihre Funktion getLocation in dem Versprechen .then zurückkehrt() und übergeben es an die Update-Funktion

function giveAddress(){ 
    $.getJSON('http://localhost:5000/api/stations', function(json) { 
    { 
    $.when($.ajax(getLocation(json))).then(function (response) { 
     update(response); 
    }); 
    }) 
} 
Verwandte Themen