【问题标题】:Javascript async function console log the returned dataJavascript 异步函数控制台记录返回的数据
【发布时间】:2017-12-30 17:01:48
【问题描述】:

如何控制日志 - 或对异步函数内部返回的数据执行任何操作?

示例: JS文件:

   async function getData(){
      try {
         $.getJSON('./data.json', (data) => {
            return data;
         });
      } catch(error) {
         console.log("error" + error);
      } finally {
         console.log('done');
      }
   }

   console.log(getData());

JSON 文件:

{
   "stuff": {
      "First": {
         "FirstA": {
            "year": [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017],
            "Categories": ["Suspension", "Electrical", "Performance", "Motor"]
         },
         "FirstB": {
            "year": [2007, 2008, 2009, 2010, 2011, 2012],
            "Categories": ["Suspension", "Electrical", "Performance", "Motor"]
         }
      },
      "Second": {
         "SecondA": {
            "year": [2002, 2003, 2004, 2005, 2006],
            "Categories": ["Suspension", "Electrical", "Performance", "Motor"]
         },
         "SecondB": {
            "year": [2007, 2008, 2009, 2010, 2011, 2012],
            "Categories": ["Suspension", "Electrical", "Performance", "Motor"]
         }
      }
   }
}

如何返回/访问 JSON 文件中的所有信息并使用它。例如,我想将“First”和“Second”添加到 div 中。与 "FirstA" 和 "FirstB" 和 "SecondA" 和 "SecondB" 相同......等等。

目前,我得到 Promise {: undefined}

任何帮助将不胜感激。

---------更新---------

如果我在函数内部运行控制台日志,我可以看到 json 数据,但我需要访问函数外部的数据。

谢尔盖

【问题讨论】:

标签: javascript json function asynchronous return


【解决方案1】:

两个问题:

  1. 设置async 函数创建的promise 的分辨率值,您必须使用async 函数本身的return 语句。您的代码在getJSON 回调(被忽略)中有一个return,而不是async 函数本身。

  2. 1234563

对于#1,你可以返回awaiting getJSON的结果:

async function getData() {
    try {
        return await $.getJSON('./data.json').promise();
    }
    catch (error) {
        console.log("error" + error);
    }
    finally {
        console.log('done');
    }
}

对于 #2,您需要 await 您的函数(这又需要在 asyncfunction 中):

console.log(await getData());

...或者通过then消费它的承诺:

getData().then(data => {
    console.log(data);
});

旁注:您的getData 隐藏错误,将它们转换为具有值undefined 的分辨率,这通常不是一个好主意。相反,请确保它传播错误:

catch (error) {
    console.log("error" + error);
    throw error;
}

然后,自然而然地,确保任何使用 getData 的东西处理或传播错误,确保某处确实处理它(否则,您会收到“未处理的拒绝”错误)。


你的评论

如何从函数外部的日志中访问 json 文件中的“东西”?

getData 的异步结果/分辨率值是 JSON 定义的对象(它不再是 JSON,它已被解析)。所以你会在上面使用.stuff,例如:

// In an `async` function
console.log((await getData()).stuff);

// Or using `then`:
getData().then(data => {
    console.log(data.stuff);
});

【讨论】:

  • T.J.第一个示例中的 Crowder i.getJSON 中的“i”是什么,它来自哪里?我知道我正在使用 getJSON 方法在其中添加一些 jQuery,但只是测试一下这是否可行。
  • @Sergio:这是一个错字(现已修复)。这很奇怪,因为我使用了复制和粘贴。所以可能不是错字,而是编辑错误。 :-)
  • 哈哈哈......这没什么大不了的。好的,刚刚测试过,现在正在发生一些事情。
  • 现在如何从函数外部的日志中访问 json 文件中的“东西”?我试过 console.log(getData().stuff);我变得不确定。
  • aahh... 所以“await”只能在异步函数内部使用。应该在异步函数之外使用“then” - 正如您在示例中明确注释的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2018-12-28
  • 2011-03-04
  • 2014-06-23
  • 2019-10-23
  • 1970-01-01
  • 2016-04-16
相关资源
最近更新 更多