preface
there was a problem of promise turning on red, green and blue lights on the Internet before. Let me describe the problem again.
topic: the red light is on once in three seconds, the green light is on once a second, and the yellow light is on once in 2 seconds. How to make the three lights turn on alternately and repeatedly? (implemented in Promse)
function red() {
console.log("red");
}
function green() {
console.log("green");
}
function blue() {
console.log("blue");
}
function middle(cb, time, time1) {
return new Promise((resolve, reject) => {
setTimeout(() => {
cb();
resolve(time1);
}, time);
});
}
async function setp1() {
const rTime = await middle(red, 3000, 1000);
const gTime = await middle(green, rTime, 2000);
const bTime = await middle(blue, gTime);
setp1();
}
setp1();