JSFiddle - React, Tailwind, and code Playground

by fiddlegrimbo

JavaScript

function getTOC() {
  var jsonStr = JSON.stringify({
    chapters: 2,
  });
  return Promise.resolve(jsonStr);
}

function getChapter(i) {
  return Promise.resolve("chapter " + i);
}

getTOC()
  .then(function(response) {
    chapters = JSON.parse(response)["chapters"];
    return chapters;
  })
  .then(function(levels) {
    var promises = [];
    for (i = 0; i < chapters; i++) {
      var promise = getChapter(i);
      promises.push(promise);
    }
    return Promise.all(promises);
  })
  .then(function(results) {
    console.log(results);
  });