JSFiddle - React, Tailwind, and code Playground
by ajithmanmu
JavaScript
// Promises approach
const f1withPromise = async() => {
return new Promise((resolve)=>{
setTimeout(()=>{
console.log('f1 with Promise');
resolve();
}, 1000)
})
};
const f2withPromise = async() => {
return new Promise((resolve)=>{
setTimeout(()=>{
console.log('f2 with Promise');
resolve();
}, 500)
})
};
const runPromises= async () => {
await f1withPromise();
await f2withPromise();
}
runPromises();
const f1 = (cb) => {
setTimeout(()=>{
console.log('1')
cb();
},1000);
}
const f2 = (cb) => {
setTimeout(()=>{
console.log('2')
cb();
},500)
}
const f3 = () => {
console.log('done')
}
// const proxy = () => f2(f3);
/* f1(proxy); */
// Or use a callback
/* f1(()=>{
proxy()
}); */
const f2Callback = () =>{
f3();
}
const f1Callback = () => {
f2(f2Callback);
}
f1(f1Callback);
/* f2(); */
const magic = (...args) => {
// console.log({args})
// Start off with assigning callback f as the last function
let f = args[args.length-1];
const iterate = (i,f) => {
return () => args[i](f);
}
for(let i=args.length-2; i >=0 ; i-=1 ) {
// f = () => args[i](f);
f = iterate(i,f)
/*
f = () =>{
current_function(f)
}
*/
}
f();
}
magic(f1,f2,f3)
console.log('finished')