test embedTumblrPosts using JS promises

embedTumblrPosts on @chrisvitalos, both featured and recent posts This fiddle will use promises

by Chris Vitalos

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en-US">
  <head>
  </head>
  <body>
    <!-- 
		.
		.
		.
		insert this 'Featured Post' HTML widget on a desirable place within the web page layout
		-->
    <h2>
      Featured Post
    </h2>
    <div id="FeaturedPost"></div>

    <!-- 
		.
		.
		.
		insert this 'Recent Posts' HTML widget on a desirable place within the web page layout 
-->
    <h2>
      Recent Tumblr Posts
    </h2>
    <div id="TumblrPosts"></div>
    <!-- 
		.
		.
		.
		Insert this Javascript reference just before the closing </body> tag 

 <script async src="https://assets.tumblr.com/post.js"></script>
	-->
  </body>
</html>

JavaScript

function embedTumblrPosts(loc, limit, tag2include, tag2ignore) {
  if (!tumblr_api_read) {
    console.log("tumblr_api_read json data block is empty!");
    return;
  }
	// -- DEBUG flag
	var DEBUG = 0;
	// -- tumblrBlogID must be pulled from Tumblr v1 API response -- 
	var tumblrBlogId = 'LeWQqtovV6GbmyI-0QBb9Q';
	// ------------------------------------------------------------
  var response = tumblr_api_read.posts;
  var len = response.length;
  var block = document.getElementById(loc);
  var embedApiHref = "https://embed.tumblr.com/embed/post/" + tumblrBlogId + "/";
  for (var i = 0; i < len; ++i) {
    var post = response[i];
    if (DEBUG) {
      console.log("id=" + post.id);
      console.log('url-slug: ' + post['url-with-slug']);
      console.log("i=" + i + " len=" + len + " limit=" + limit);

    }
    if (typeof post.tags !== 'undefined') {
      if (DEBUG) {
        console.log('post tag array:' + post.tags);
      }
      if (post.tags.includes('hidden')) {
        if (DEBUG) {
          console.log('--Post Flagged to be hidden!');
        }
        continue;
      }
      if (tag2ignore !== null && post.tags.includes(tag2ignore)) {
        if (DEBUG) {
          console.log('--Post Flagged to be ignored! ' + tag2ignore);
        }
        continue;
      }
      if (tag2include !== null && post.tags.includes(tag2include) === false) {
        if (DEBUG) {
          console.log('--Post tag not in whitelist! ' + tag2include);
        }
        continue;
      }
    } else {
      if (tag2include !== null) {
        if (DEBUG) {
          console.log('--No post tag(s) and include tag specified! ' + tag2include);
        }
        continue;
      }
    }
    dataHref = embedApiHref + post.id;
    var recent = document.createElement('div');
    recent.setAttribute("class", "tumblr-post");
    recent.setAttribute("data-href", dataHref);
    var html = "&lt;noscript&gt;&lt;li&gt;&lt;a href='" +
      (post['url-with-slug'] || post.url) + "'&gt;" +
     ...