JSFiddle - React, Tailwind, and code Playground

by Sujit Kumar Singh

JavaScript

function condition(){
    // This function is not accessible to me.
    // Return promise after 1 sec
    return new Promise(function(resolve, reject){
        setTimeout(function(){
            if(parseInt(Math.random() * 100) % 2){
                resolve(true);
            }
            else{
                reject(false)
            }
        }, 1000)
    });
}

function delayIncrease(value, index){            
    condition().then(
        function(){ updateData(++value, index) },
        function(){ updateData(--value, index) }
    )
}

function updateData(value, index){
    // Update value of dataArr"
    dataArr[index] = value;

    document.write(dataArr, '<br>'); // Print to page            
}

dataArr = [1,2,3,4,5];
for(var i in dataArr){
    dataArr[i] = delayIncrease(dataArr[i], i);
}