如何从异步回调函数返回值? [重复]

本文翻译自:How to return value from an asynchronous callback function? [duplicate]

This question already has answers here : 这个问题已经在这里有了答案
How do I return the response from an asynchronous call? 如何从异步调用返回响应? (36 answers) (36个答案)
Closed 4 years ago . 4年前关闭。

This question is asked many times in SO. 在SO中多次问过这个问题。 But still I can't get stuff. 但是我还是收不到东西。

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

#1楼

参考:https://stackoom.com/question/SjOj/如何从异步回调函数返回值-重复


#2楼

It makes no sense to return values from a callback. 从回调返回值没有意义。 Instead, do the "foo()" work you want to do inside your callback. 而是回调中执行您要执行的“ foo()”工作。

Asynchronous callbacks are invoked by the browser or by some framework like the Google geocoding library when events happen. 发生事件时,浏览器或Google地理编码库等框架会调用异步回调。 There's no place for returned values to go. 没有地方可以返回值。 A callback function can return a value, in other words, but the code that calls the function won't pay attention to the return value. 换句话说,回调函数可以返回一个值,但是调用该函数的代码不会注意返回值。


#3楼

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 . 如果您有很多回调,则可以考虑尝试并使用诸如Q的promise库 。


#4楼

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. 它允许您将回调函数的执行推迟到ajax请求(或任何异步操作)完成之前。 This can also be used to call a callback once several ajax requests have all completed. 一旦几个ajax请求全部完成,它也可以用来调用回调。

你可能感兴趣的:(javascript,asynchronous,callback)