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)
    });
}

async function delayIncrease(value){
    // return calculated value depending on promise return
    var choice = await condition(); // "condition().then()" won't fulfill requirement

    if(choice) { return ++value; }
    else { return --value; }
}

// Start calling functions
dataArr = [1,2,3,4,5];
for(var i in dataArr){
    dataArr[i] = delayIncrease(dataArr[i]);

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