我发誓这种语言毫无意义。 Promise 执行器立即运行,在这种情况下,它调用解析函数,但尚未附加解析处理程序:
// FLOW STARTS HERE
let promise = new Promise(function(resolve, reject) {
// THIS IS THE EXECUTOR, RUN IMMEDIATELY ON PROMISE CONSTRUCTION
// THE PROMISE CONSTRUCTOR CALLS THIS FUNCTION, WITH WHAT ARGUMENTS?
console.log("inPromise");
resolve(value); // here, what is called?
console.log("logged again"); // this is logged
});
// CONTROL FLOW CONTINUES
promise.then(() => console.log("promise fulfilled"));
console.log("hello");
resolve
函数只是将promise的内部状态设置为“已解决”。如果已经设置了一个或多个 .then
回调,它们将被调用。如果没有,没问题,你只是有一个处于已解决状态的承诺。稍后,如果您对已解决的 Promise 调用 .then
,您的回调将被执行。