JSFiddle - React, Tailwind, and code Playground

by chintsu

HTML

<div id='console'></div>

JavaScript

function log(msg) {
    var $out=$("<div />");
    $out.html(msg);
    $("#console").append($out);
}

var array= ['a', 'b', 'c', 'd', 'e', 'f'],
    length = array.length,
    limit = 3,
    addedCount = 0,
    i = 0;

fetch();

function fetch() {
 log(array[i] + ' started');
 
 if(addedCount == limit) {
  log(array[i] +' cancelled ');
     
  done();
  return;
 }
    
 $.ajax({
    type: "GET",
    url: "/echo/json/",
    data: {label:array[i]},
    dataType: "JSON",
    success: function() {
        addedCount++;
        log(array[i] + ' finished');
        i++;
        fetch();
    }
 });                                              
}

function done(){
 log('All finished');
};