Generic JavaScript Promise Chain

by Shawn Wood

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

JavaScript

// Define the promiseChain array
var promiseChain = [];
// Define another promise chain array
var purposeMissionChain = [];

var chainFunction1 = function() {
  console.info(arguments.callee.name);

};
var chainFunction2 = function() {
  console.info(arguments.callee.name);

};
var chainFunction3 = function() {
  console.info(arguments.callee.name);

};
var chainFunction4 = function() {
  console.info(arguments.callee.name);

};
var chainFunction5 = function() {
  console.info(arguments.callee.name);
  purposeMissionChain.push(chainFunction6());
  purposeMissionChain.push(chainFunction7());
};
var chainFunction6 = function() {
  console.info(arguments.callee.name);

};
var chainFunction7 = function() {
  console.info(arguments.callee.name);

};
var chainFunction8 = function() {
  console.info(arguments.callee.name);

};
/** Set the recursive promise chain that will 
 * loop through the promises until they are complete
 * then run the final promise to end the chain.
 * @function
 * @name recursivePromiseChain
 */
var promiseChainCompletion = function() {
  console.info(arguments.callee.name);
  // Run the final function
  chainFunction4();
};
/** Generic function to build a promise chain that will 
 * loop through the promises until they are complete
 * then run the final promise to end the chain.
 * @function
 * @name DDSF.Utils.promiseChainActions
 * @param {Object[]} chainArray - promise chain array
 * @param {Object} startFunction - The function that kicks the promises off
 * @param {Object} endFunction - The function that ends the chain
 */
var promiseChainActions = function(chainArray, startFunction, endFunction) {
  if (typeof chainArray === 'undefined' || chainArray === null) {
    chainArray = [];
  }
  if (typeof startFunction === 'undefined' || startFunction === null) {
    startFunction = function() {};
  }
  if (typeof endFunction === 'undefined' || endFunction === null) {
    endFunction = function() {};
  }
  /** Set the recursive promise chain that...