小编Jay*_*oto的帖子

可以将此代码重写为异步函数吗?

我需要一个在转换完成时返回承诺的元素。下面的代码工作正常,但是,我想知道是否可以用完整的async/await语法重写此代码。

const box = document.querySelector(".box");

const animate = () =>
  new Promise((resolve) => {
    box.classList.add("animate");
    box.addEventListener("transitionend", () => resolve());
  });

box.addEventListener(
  "click",
  async() => {
    await animate();
    console.log("done animating!");
  }, {
    once: true
  }
);
Run Code Online (Sandbox Code Playgroud)
.box {
  width: 100px;
  height: 100px;
  background-color: blue;
  transition: width 3s linear;
}

.box.animate {
  width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
<div class="box"></div>
Run Code Online (Sandbox Code Playgroud)

javascript css async-await

1
推荐指数
1
解决办法
61
查看次数

标签 统计

async-await ×1

css ×1

javascript ×1