The problem of promise red, green and blue lights realized by async/await

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();

Mar.05,2022

let lights = [
    {name: 'green', time: '3'},
    {name: 'yellow', time: '2'},
    {name: 'red', time: '3'},
];

function changeLight(item) {
    return new Promise((resolve) => {
        console.log(item.name);
        setTimeout(() => {
            resolve();
        }, item.time * 1000)
    })
}

async function action(index) {
    var item = lights[index];
    await changeLight(item);
    index = PPindex >= lights.length ? 0 : index;
    action(index)
}

action(0)
Menu