JSFiddle - React, Tailwind, and code Playground
by ronnjoe
JavaScript
function* rainbow() {
// the asterisk marks this as a generator
yield 'red';
yield 'orange';
yield 'yellow';
yield 'green';
yield 'blue';
yield 'indigo';
yield 'violet';
}
/* for(let color of rainbow()) {
console.log(color);
} */
var array = [
function(callback) {
console.log("first function called in " + (new Date().getTime() - timestamp) + "ms");
callback();
},
function(callback) {
console.log("second function called in " + (new Date().getTime() - timestamp) +
"ms");
callback();
},
function(callback) {
console.log("third function called in " + (new Date().getTime() - timestamp) + "ms");
callback();
}
];
timestamp = new Date().getTime();
//Created a global object
async function flows(array, callback){
flow();
async function returnSelf(item) { return await item(callback); }
async function flow() {
return await array.map(returnSelf);
}
}
/* (async function() {
console.log(await flow(array, returnSelf));
})(); */
var myApp = {
isFunctionCompleted : false //Assigned boolean false
};
//function *flow(array, callback) {
//const res1 = yield array[0]();
//res1.next();
//var trivialGenerator = function *(array) {
//for (var item of array) {
// some item manipulation
// var abc = yield item;
//return abc;
//abc.next();
//}
//}
/* array.forEach(function(item, index) {
//Condition to check, if the current item is a last index of the array
//var abc = "res"+index;
if((array.length -1) === index){
// If last index, change the value of the global myApp variable to true
myApp.isFunctionCompleted = true
}
//item(callback);
});*/
//}
//(async function() {
(async function() {
await flows(array, function() {
//Print the log only if the condition is statisfied
//if(myApp.isFunctionCompleted){
console.log("all functions finished in " + (new Date().getTime() - timestamp) + "ms");
//}
});
//console.log(await flow(array, returnSelf));
})();
//console.log(it.next());