【发布时间】:2015-12-01 20:42:58
【问题描述】:
我有以下代码:
//calling AsyncFunction
var coords = LocationSerivice.getLocation();
var localStores;
setTimeout(function(){
//works
console.log(coords);
//calling async function again
localStores = LocalStoresService.getLocalStores(coords[0],coords[1]);
}, 100);
setTimeout(function(){
//doesn't work
console.log(localStores);
}, 100);
我想要做的是首先调用一个返回我的位置的异步函数。然后为了从该函数中获取数据,我设置了超时。收到数据后,我正在调用第二个异步函数。我再次设置超时以从我的第二个异步函数中获取数据,所以这里失败了。如果我尝试在超时块中显示带有console.log() 的数据,它会显示undefined 而console.log() 在第一个超时块中有效。
我也尝试过增加超时时间,但它不起作用。 有什么建议可以克服吗?
【问题讨论】:
-
您的标签之一是 angularjs,所以我建议您查看
promises stackoverflow.com/questions/15604196/…
标签: javascript angularjs asynchronous ionic