我在一篇文章中读到,诺言中的处理程序下面的代码是首先执行的(异步执行代码)
let promise = Promise.resolve();
promise.then(() => alert("promise done!"));
alert("code finished"); // this alert shows first
上面的代码首先警告“代码完成”。为了解决这个问题,下面是他们提供的解决方案
Promise.resolve()
.then(() => alert("promise done!"))
.then(() => alert("code finished"));
我的问题是实时性问题,这些处理程序下面可能有1000行代码。我们不能在then处理程序中处理所有操作。如何同步处理]
阅读有关async/await
。这是一种以同步方式编写异步代码的方式。