在继续之前等待异步函数返回数据

时间:2011-07-05 13:17:15

标签: javascript jquery asynchronous

我正在调用我创建的异步函数,并在其调用代码中,希望此函数完成并返回其结果,然后在调用此异步函数后继续处理其余代码。这可能吗?

示例代码:

alert('We\'re starting!');
data = calling_asynchronous_function();
window.location.replace(www.google.com);
// 1000 more lines of code that handles data from asynchronous function

下面是我正在使用的方法,但这意味着我可以使用异步代码创建一个函数...

当前方法:

alert('We\'re starting!');
asynch_function({'parameter1': value1}, function() {
    // 1000 lines of code that handles data from asynchronous function
});

对于current method,您可以看到我无法将代码转换为可重复使用的函数,并且如果我想使用类似的东西,则必须在任何地方复制代码...

0 个答案:

没有答案
相关问题