2017-05-27 10 views
0

は、私は(私が剥離し、簡略化していること)、次のコードを持っている:JavaScriptコールバック関数を最初に実行するにはどうすればよいですか?

function myOrder() { 
     setLocation(); // sets a value for global variable 'location' 
         // using GET   

     $.post('http://111.111.111.111/order', { 
       Item: Chair, 
       Price: 50, 
       Location: location; 
               }) 
} 

問題は、この関数はのsetLocation()コールバック関数が終了する前にポスト()のコードを実行していることです。

最初に終了するにはsetLocation()関数が必要なので、 'location'のグローバル変数を割り当てることができます。これはPOST部分の値として使用されます。

これを行うことができますか?

おかげ

編集:明確にする: のsetLocation()GPSとグローバル変数名「場所」の下に格納します経由して現在位置を取得するGET要求、です。

私は、コールバック関数の前にmyOrder()メソッドPOSTSがGET応答を終了したと思います。

+4

あなたが実装を追加することができます'setLocation'の? – trincot

+0

[Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)を見てください。関数setLocationは1を返す必要があります。 – Ricky

+0

setLocationは、関数が正常に実行されたときに解決されるPromiseを返します。投稿要求を.then()ハンドラに追加することができます。 –

答えて

1

あなたがsetLocationの実装を提供しなかったとして、私はあなたが$.getまたはいくつかの類似したjQueryの機能($.ajax$.getJSON、...)を使用すると仮定します。その場合

、約束ですので$.getのコールバック関数を使用していない、と応答データにグローバル変数を格納しませんが、リターン$.get(...)

function setLocation() { 
    // Return the promise object returned by $.get, $.getJSON, $.ajax, ... 
    // instead of using the callback function, and don't store the result 
    // in a global variable 
    return $.get("myurl").then(function (data) { 
     // As you did not provide the code, this serves only as example. The 
     // way you extract the latitude and longitude from the response may be 
     // different. Adapt as needed: 
     data = JSON.parse(data); 
     var lat = data.result[0].lat; 
     var lng = data.result[0].lng; 
     // Return value will be the promised value: adapt the format as needed, 
     // This will become the location variable content in your other function 
     return { lat, lng }; 
    }); 
} 
function myOrder() { 
    // Use the `then` method on the jQuery promise, and return 
    // the result of the `then` method again, so you can chain 
    // on myOrder() as well... 
    return setLocation().then(function (location) { 
     // Return the `$.post` result: 
     return $.post('http://111.111.111.111/order', { 
      Item: Chair, 
      Price: 50, 
      Location: location; 
     }); 
    }); 
} 
+0

こんにちは、ありがとう。しかし、私のsetLocation()関数は何も返しません。 openstreetmapを使って場所を取得し、グローバル変数 'location'を経度に変換した住所に設定します。 setLocation()が何も返さない場合、私はどのように実装しますか?ありがとうございました – teep

+0

私のポイントは、* setLocation'を変更して何かを返すようにし、グローバル変数を設定しないことです。 – trincot

+0

うーん、私は参照してください。私のsetLocation()メソッドは、2つのグローバル変数を設定します(longitudeとlatituteの場合)。メソッドでこれらの2つを返すにはどうすればよいでしょうか?私の$ .postコードは実際にこれらの2つの情報を投稿します。 – teep

2

使用非同期流れ

function setLocation() { 
    return new Promise((resolve, reject) => { 
    // I do my stuff 

    // I finished 
    resolve(); 
    }); 
} 

function myOrder() { 
     setLocation().then(() => { 
      $.post('http://111.111.111.111/order', { 
        Item: Chair, 
        Price: 50, 
        Location: location; 
               })   
     }) 
} 
0

setLocation関数は非同期関数である場合、あなたはそれが完了した後にコールバック関数を受け取るためにそれを変更する必要があります。

function myOrder() { 
    setLocation(function() { 
     $.post('http://111.111.111.111/order', { 
      Item: Chair, 
      Price: 50, 
      Location: location 
     }); 
    }); 
} 

希望します。

+0

これは機能しませんでした。 setLocation()メソッドのヘッダーで何かを変更する必要がありますか? – teep

+0

@teep厳密には、コールバック引数を受け取るようにsetLocation関数を変更する必要があります。実行が終了すると、コールバックをトリガする必要があります。次のようなものになります: 'function setLocation(callback){doMyStuffHere();折り返し電話(); } ' –

+0

まだ動作しません!それは良いなどを構築するが、$ .postはまだ最初に実行されます。 setLocationメソッドでアラートを設定し、$ .postアラートの後に表示します。何か案が?編集:固定、私が作成したIFステートメントにコールバック()を配置する必要がありました。ありがとう – teep

関連する問題