How to run then() when all json data gone through the loop

我的未来我决定 提交于 2019-12-13 03:27:12

问题


In my code I deal with multiple JSON requests that need to be parsed in an order.

let allRadio_data, allHistory_data, iTunes_data;


$.when(parseRadioList())
  .then(function(list) {
    radioList_data = list;

    return $.when(
    parseEachRadioData(radioList_data), 
    ParseEachHistoryData(radioList_data)
    );

  })
  .then(function() {
    console.log(allRadio_data);
    console.log(allHistory_data);

    return $.when(_parseiTunes());
  })
  .then(function(iTunesInfo) {
    iTunes_data = iTunesInfo;

    return _cacheOptions();
  })

function _cacheOptions() {
  // FINAL function
}


/////////
function parseRadioList() {
  return $.getJSON("https://api.myjson.com/bins/xiyvr");
}

function _parseiTunes() {
  return $.getJSON("https://itunes.apple.com/search?term=jackson&limit=10&callback=?")
}

function parseEachRadioData(radioList) {
  allRadio_data = [];
  $.each(radioList, function(index, radio) {
    $.when($.getJSON(radio.url + "/stats?sid=" + radio.stream_id + "&json=1&callback=?"))
      .then(function(data) {
        allRadio_data.push(data);
      });
  })
}

function ParseEachHistoryData(radioList) {
  allHistory_data = [];
  $.each(radioList, function(index, radio) {
    $.when($.getJSON(radio.url + "/played?sid=" + radio.stream_id + "&type=json&callback=?"))
      .then(function(data) {
        allHistory_data.push(data);
      });
  })
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Right now the code is running, but where I do console.log(allRadio_data); it is empty. However, if I do settimeout() to delay it for a second the data is completed. This means then() is not running on time.

This is the structure I am looking for:

  1. JSON file 1 is parsed. parseRadioList()
  2. JSON1 is an array of multiple entries of JSON URLS.
  3. Run through the URLs within JSON1 array and do getJSON for each. parseEachRadioData(radioList_data) & ParseEachHistoryData(radioList_data)
  4. Push data of each JSON in one general Array.
  5. Once completed, parse JSON2 _parseiTunes()

Any Idea how to make this code running in the right structure.

Thanks in advance.


回答1:


For a start, parseEachRadioData and ParseEachHistoryData don't return anything at all, let alone a Promise - so it's impossible to wait on them

Also, you're overusing $.when ... in fact you never need to use it, just use regular promises since jQuery $.getJSON etc return a usable Promise-like object

i.e. your code could be

let allRadio_data, allHistory_data, iTunes_data;


parseRadioList()
.then(function(list) {
    radioList_data = list;
    return Promise.all([
        parseEachRadioData(radioList_data), 
        ParseEachHistoryData(radioList_data)
    ]);

})
.then(function(result) {
    allRadio_data = result[0];
    allHistory_data = result[1];
    console.log(allRadio_data);
    console.log(allHistory_data);
    return _parseiTunes();
})
.then(function(iTunesInfo) {
    iTunes_data = iTunesInfo;
    return _cacheOptions();
})

function _cacheOptions() {
  // FINAL function
}


/////////
function parseRadioList() {
    return $.getJSON("https://api.myjson.com/bins/xiyvr");
}

function _parseiTunes() {
    return $.getJSON("https://itunes.apple.com/search?term=jackson&limit=10&callback=?")
}

function parseEachRadioData(radioList) {
    return Promise.all(radioList.map(radio => $.getJSON(radio.url + "/stats?sid=" + radio.stream_id + "&json=1&callback=?")));
}

function ParseEachHistoryData(radioList) {
    return Promise.all(radioList.map(radio => $.getJSON(radio.url + "/played?sid=" + radio.stream_id + "&type=json&callback=?")));
}



回答2:


from a first view

$.when(parseRadioList())  // your code will wait her
  .then(function (list) {
    radioList_data = list;

    return $.when(
      parseEachRadioData(radioList_data), //  this two method are void they will complete 
      ParseEachHistoryData(radioList_data)  // immediately without waithing for getJson...
    );

  })


来源:https://stackoverflow.com/questions/57582152/how-to-run-then-when-all-json-data-gone-through-the-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!