JSFiddle - React, Tailwind, and code Playground
JavaScript
const genRandom = () => {
const random = Math.floor(Math.random() * 5);
console.log('generate: ', random);
return random;
}
const asyncFunc = (delay, index) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(index);
}, delay * 1000);
});
}
const asyncRecursion = async (retry) => {
const result = await asyncFunc(genRandom(), retry);
console.log('result: ', result);
if (retry > 0) await asyncRecursion(--retry);
return true;
}
asyncRecursion(10).then(() => {
console.log('finished.');
});