Refactoring callback hell code

 let h1 = document.querySelector("h1");

function changeColor(color, delay) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      h1.style.color = color;
     resolve("color changed");
    }, delay);
  });
}

changeColor("red", 1000)
  .then(() => {
    console.log("Color changed to Red");
    return changeColor("orange", 1000);
  })
  .then(() => {
    console.log("color changed to orange");
    return changeColor("green", 1000);
  })
  .then(() => {
    console.log("color changed to green");
    return changeColor("blue", 1000);
  })
  .then(() => {
    console.log("color changed to blue");
  });



Comments

Popular posts from this blog

Callback Hell

TODO list with DOM