2017-05-27 93 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()是一个GET请求,即一个下全局变量名“位置”得到通过GPS并将其存储在当前位置。

所以我认为是在回调函数完成其GET响应之前myOrder()方法POSTS。

+4

您可以添加实施'setLocation'? – trincot

+0

查看[Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)。函数setLocation应该返回一个。 – 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获取位置,并将“位置”的全局变量设置为转换为经度的地址。如果setLocation()不返回任何东西,我将如何实现这一点?谢谢 – teep

+0

我的观点是你应该改变setLocation以便它返回一些东西,而不是设置一个全局变量(这是一个坏习惯)。 – trincot

+0

嗯,我明白了。我的setLocation()方法设置了两个全局变量(经度和纬度)。我将如何使这个方法返回这两个?我的$ .post代码实际上发布了这两位信息。 – 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