JSFiddle - React, Tailwind, and code Playground
by ppgab
JavaScript
class Myclass{
constructor(t){
this.time = t
}
load(){
return new Promise(r => setTimeout(f => r(this.time), this.time));
}
}
const objs = [];
//Using random to simulate dynamic amount of promises
for(i=1; i<getRandomInt(5,10);i++){
objs.push(new Myclass(1000/i+1))
}
console.log(objs);
let start = 0;
function loadObjsSequence(obj){
if(start < objs.length){
return obj.then(() => loadObjsSequence(objs[start+1]));
}else{
return obj
}
}
loadObjsSequence(objs[start].load())
.then(t => {
console.log(t);
})
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}