Javascript 如何从异步回调函数返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6847697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return value from an asynchronous callback function?
提问by Gowri
This question is asked many times in SO. But still I can't get stuff.
这个问题在 SO 中被问了很多次。但是我还是拿不到东西。
I want to get some value from callback. Look at the script below for clarification.
我想从回调中获得一些价值。请查看下面的脚本以进行澄清。
function foo(address){
// google map stuff
geocoder.geocode( { 'address': address}, function(results, status) {
results[0].geometry.location; // I want to return this value
})
}
foo(); //result should be results[0].geometry.location; value
If I try to return that value just getting "undefined". I followed some ideas from SO, but still fails.
如果我尝试返回该值,只会得到“未定义”。我遵循了 SO 的一些想法,但仍然失败了。
Those are:
那些是:
function foo(address){
var returnvalue;
geocoder.geocode( { 'address': address}, function(results, status) {
returnvalue = results[0].geometry.location;
})
return returnvalue;
}
foo(); //still undefined
回答by Sean Kinsey
This is impossible as you cannot return from an asynchronous call inside a synchronous method.
这是不可能的,因为您不能从同步方法内的异步调用返回。
In this case you need to pass a callback to foo that will receive the return value
在这种情况下,您需要将回调传递给将接收返回值的 foo
function foo(address, fn){
geocoder.geocode( { 'address': address}, function(results, status) {
fn(results[0].geometry.location);
});
}
foo("address", function(location){
alert(location); // this is where you get the return value
});
The thing is, if an inner function call is asynchronous, then all the functions 'wrapping' this call must also be asynchronous in order to 'return' a response.
问题是,如果一个内部函数调用是异步的,那么所有“包装”这个调用的函数也必须是异步的,以便“返回”一个响应。
If you have a lot of callbacks you might consider taking the plunge and use a promise library like Q.
回答by Pointy
It makes no sense to return values from a callback. Instead, do the "foo()" work you want to do insideyour callback.
从回调中返回值是没有意义的。相反,在回调中执行您想要执行的“foo()”工作。
Asynchronous callbacks are invoked by the browser or by some framework like the Google geocoding library when events happen. There's no place for returned values to go. A callback function canreturn a value, in other words, but the code that calls the function won't pay attention to the return value.
当事件发生时,浏览器或某些框架(如 Google 地理编码库)会调用异步回调。没有返回值的地方。回调函数可以返回一个值,换句话说,但是调用该函数的代码不会关注返回值。
回答by Alex Heyd
If you happen to be using jQuery, you might want to give this a shot: http://api.jquery.com/category/deferred-object/
如果您碰巧使用 jQuery,您可能想试一试:http: //api.jquery.com/category/deferred-object/
It allows you to defer the execution of your callback function until the ajax request (or any async operation) is completed. This can also be used to call a callback once several ajax requests have all completed.
它允许您推迟回调函数的执行,直到 ajax 请求(或任何异步操作)完成。这也可用于在多个 ajax 请求全部完成后调用回调。