【问题标题】:javascript use response from function in calling functionjavascript在调用函数时使用函数的响应
【发布时间】:2014-06-26 12:35:06
【问题描述】:

我有一个节点地理编码器的基本示例,但我无法得到响应以冒泡到调用函数中

    var address = userUpdates.address + " " + userUpdates.city + ", " + userUpdates.state + " " + userUpdates.zip;

   geocoder.geocode(address)
        .then(function(res) {
              console.log(res);
        })
        .catch(function(err) {
             console.log(err);
       });

console.log 生成正确的 JSON

[ 
  { 
    latitude: 35,
    longitude: -78.5,
    country: 'United States',
    city: 'Clayton',
    state: 'North Carolina',
    stateCode: 'NC',
    zipcode: '27520',
    streetName: 'Main St',
    streetNumber: '100',
    countryCode: 'US' 
   }
 ]

但我无法将 res 分配给将在 geocoder.geocode 函数之外持续存在的任何内容

【问题讨论】:

    标签: javascript node.js geocode


    【解决方案1】:

    您是否尝试过在 geocoder.geocode 函数之外创建一个全局变量来保存该值?

       var globalRes = null;
       geocoder.geocode(address)
            .then(function(res) {
                  globalRes = res;
                  console.log(res);
            })
            .catch(function(err) {
                 console.log(err);
           });
    

    【讨论】:

    • 我做了,当我在地理编码函数之外调用它时变量为空。
    • 嗯...你确定你是在 geocoder.geocode() 完成之后而不是在它完成之前调用 globalRes 变量吗?
    • 我直接复制了你的代码并在函数.then之外添加了一个发送console.log。它返回 NULL
    • geocode函数是异步的,因此即使在geocode函数之后使用了globalRes,并不意味着geocode函数已经真正完成。尝试使用 setInterval() 检查 globalRes 的值,看看几秒钟后该值是否发生变化。比如:var intervalId = 0; function isDone() { if (globalRes) { clearInterval(intervalId); alert('We have it!') } } intervalId = setInterval(isDone, 1000);
    • 谢谢。我想我现在明白了。在 isDone 函数中运行更新/保存替换警报似乎有效。
    【解决方案2】:

    我认为你应该能够做这样的事情:-

    var response = null;
    geocoder.geocode(address)
        .then(function(res) {
              response = res;
              console.log(res);
        })
    

    【讨论】:

      【解决方案3】:

      欢迎来到回调的世界。

      这是一个简化的代码示例,可以帮助您入门。

      function myBigFunction() {
        // Here is part 1:
        console.log("I am part 1");
      
        // Now, call something async and wait for the callback
        myAsyncFunction(function (result) {
          console.log("I am part 2, with the results from myAyncFunction");
        });
      
        function myAsyncFunction(callback) {
          var returnValue = "This is returned";
      
          window.setTimeout(function () {
            // Call the callback and make myBigFunction pick back up.
            callback(returnValue);
          }, 250);
      
        }
      
      }
      myBigFunction();
      

      因此,在您的示例中,您需要以下内容:

      function doGeoCoder(callback) {
         geocoder.geocode(address)
              .then(function(res) {
                    callback(res);
                    console.log(res);
              })
              .catch(function(err) {
                   console.log(err);
             });
      }
      
      doGeoCoder(function(res) {
        console.log("I'm in the calling function's callback with");
        console.log(res);
      });
      

      【讨论】:

      • 如何在 doGeoCoder 函数之外引用 console.log(res) 中的 res?它包含一个带有纬度和经度的 JSON 对象。我想将它们添加到用户更新并保存到数据库中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多