pvitkovsky
5/29/2019 - 2:04 PM

JS Bin// source https://jsbin.com/gudoqacibu

https://tylermcginnis.com/async-javascript-from-callbacks-to-promises-to-async-await/
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Callback-To-Promise</title>
</head>
<body>

<script id="jsbin-javascript">
console.clear();
//Callback with settimeout; 
function go(times, callback) { //higher-order fun
    return setTimeout(() => { // returning it invoked
        while (times > 0) {
            times--;
            callback(times);
        }
    }, 200);
}


function goReturns(times, callback) {
    return function() { // or returning fun and do setTimeout in client;
        while (times > 0) {
            times--;
            callback(times);
        }
    }
}

function print(num) {
    console.log("Number is " + num);
}
//client  to go
go(5, print);
//client to goReturns;
setTimeout(() => goReturns(3, print)(), 1600);
console.log('printint in timeout')

setTimeout(() => {
    const lol = new Promise((resolve, reject) => {
        setTimeout(() => resolve(), 300);
    })

    const oldz = new Promise((ok, cancel) => {
        ok();
    });

    lol.then(goReturns(5, print));
    oldz.then(console.log('YOU ARE SUPERIOR'));
}, 2000);
</script>



<script id="jsbin-source-javascript" type="text/javascript">console.clear();
//Callback with settimeout; 
function go(times, callback) { //higher-order fun
    return setTimeout(() => { // returning it invoked
        while (times > 0) {
            times--;
            callback(times);
        }
    }, 200);
}


function goReturns(times, callback) {
    return function() { // or returning fun and do setTimeout in client;
        while (times > 0) {
            times--;
            callback(times);
        }
    }
}

function print(num) {
    console.log("Number is " + num);
}
//client  to go
go(5, print);
//client to goReturns;
setTimeout(() => goReturns(3, print)(), 1600);
console.log('printint in timeout')

setTimeout(() => {
    const lol = new Promise((resolve, reject) => {
        setTimeout(() => resolve(), 300);
    })

    const oldz = new Promise((ok, cancel) => {
        ok();
    });

    lol.then(goReturns(5, print));
    oldz.then(console.log('YOU ARE SUPERIOR'));
}, 2000);</script></body>
</html>
console.clear();
//Callback with settimeout; 
function go(times, callback) { //higher-order fun
    return setTimeout(() => { // returning it invoked
        while (times > 0) {
            times--;
            callback(times);
        }
    }, 200);
}


function goReturns(times, callback) {
    return function() { // or returning fun and do setTimeout in client;
        while (times > 0) {
            times--;
            callback(times);
        }
    }
}

function print(num) {
    console.log("Number is " + num);
}
//client  to go
go(5, print);
//client to goReturns;
setTimeout(() => goReturns(3, print)(), 1600);
console.log('printint in timeout')

setTimeout(() => {
    const lol = new Promise((resolve, reject) => {
        setTimeout(() => resolve(), 300);
    })

    const oldz = new Promise((ok, cancel) => {
        ok();
    });

    lol.then(goReturns(5, print));
    oldz.then(console.log('YOU ARE SUPERIOR'));
}, 2000);
//creating nested JSON object from a set of arrays
var tree = {}

function addToTree(tree, array) { 
   for (var i = 0, length = array.length; i < length; i++) {
       tree = tree[array[i]] = tree[array[i]] || {}
   }
}

addToTree(tree, ["a", "b", "c"])
addToTree(tree, ["a", "b", "d"])
       
       console.log(tree);
       
       
//Purified JSON builder
<script>
function getObjectFormat(title,payload){
    return {
        title : payload
    };
};

function createChildRows(array,payload){
    const len = array.length;
    
    var formate = getObjectFormat(array[len-1],payload);
    for(var i = len-2; i >= 0; i--){
        formate = getObjectFormat(array[i],formate);
    };
    return formate;
};

// Demo call. Console the result of generated dynamic Json Objects

var array = ("test1|test2|test3|test4").split("|");

console.log(JSON.stringify(createChildRows(array,{"title":"Last Child Row"})));
</script>