【问题标题】:How to get JSON files one after other when each downloaded每次下载时如何一个接一个地获取JSON文件
【发布时间】:2019-08-06 20:34:14
【问题描述】:

我正在尝试首先编写一个插件,

  1. 它会加载 2 个 json 文件,并将每个文件保存在一个变量中,
  2. 然后再加载 2 个 json 文件并将它们保存在变量中,
  3. 然后运行另一个函数。

这三步需要在上一步完成后进行。主要原因是上一步获取的信息将用于下一步。

我有这段代码,但不能按我想要的方式工作。

 $.when(parseJSON1(), parseJSON2())
  .then(
    parseJSON3(station_data.dj), parseJSON4(station_data.songurl)
  )
  .then(
    _cacheOptions
  )

  });

  var station_data, history_data, itunes_data, coverList_data;

  // Core Functions

  function _cacheOptions() {
    station_data = stationInfo[0];
    history_data = stationHistory[0];
    itunes_data = itunesInfo[0];
    coverList_data = coverInfo[0];
  }

  // Functions

  function parseJSON1() {
    return $.getJSON(settings.JSON1);
  }

  function parseJSON2() {
    return $.getJSON(settings.JSON2);
  }

  function parseJSON3(searchTerm) {
    return $.getJSON(settings.JSON3);
  }

  function parseJSON4() {
    return $.getJSON(settings.JSON4);
  }

【问题讨论】:

    标签: javascript jquery json async-await getjson


    【解决方案1】:

    根据其documentation,.then() 方法将其参数中的两个回调分别视为在成功或失败/错误的情况下调用的函数。你的代码目前的编写方式,看起来就像你在告诉它:

    • 获取 JSON1 和 JSON2
    • 如果这两个调用成功解析,则获取 JSON3
    • 如果任一调用失败,则获取 JSON4
    • 然后调用_cacheOptions

    除此之外,似乎还有其他一些问题:

    • 在 .when() 子句中获取的数据没有传递给需要第一步信息的两个回调。
    • _cacheOptions 函数引用了未定义的变量stationInfo、stationHistory、itunesInfo、coverInfo

    假设这四个变量是 parseJSON1 到 4 获取的数据,您可以将代码重写为:

    var stationInfo, stationHistory, itunesInfo, coverInfo; // to store the returned JSON data
    
    var station_data, history_data, itunes_data, coverList_data; // set by _cacheOptions
    
    // wait for getJSONs to resolve
    $.when(parseJSON1(), parseJSON2()) 
    
      // use resolved data in next step
      .then(function(JSON1, JSON2) {
    
        // set your scoped vars
        stationInfo = JSON1;
        stationHistory = JSON2;
    
        // Then call the next two async fetches
        // Note that, since you already set your local vars with the returned data,
        // you don't need to pass anything to these functions, they can just
        // reference the info like the _cachOptions function
        return $.when(parseJSON3(), parseJSON4());
      })
      .then(function(JSON3, JSON4){
    
        // set your scoped vars
        itunesInfo = JSON3;
        coverInfo = JSON4;
    
        return _cacheOptions();
      })
    
    // Core Functions
    function _cacheOptions() {
      station_data = stationInfo[0];
      history_data = stationHistory[0];
      itunes_data = itunesInfo[0];
      coverList_data = coverInfo[0];
    }

    【讨论】:

    • 感谢您的回复。很好的解决方案。就一个问题。能否告诉我链条如何继续?我的意思是有多个函数必须在_cacheOptions 之后运行。使用我的初始代码,我尝试对每个代码使用then()。如:.then( _cacheOptions ) .then( _cacheDom ) .then( _events ).then( _render );。所以每个函数一个接一个地运行。
    • 是的,您可以使用 .then() 以这种方式链接任意数量的回调。您还可以查看异步函数,这些函数通常可以让您使用更清晰的代码来做同样的事情。 (见hackernoon.com/…medium.com/@bluepnume/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    相关资源
    最近更新 更多