Promise的一点疑惑?
11 个回答
无论 resolve 的参数是不是 thenable,Promise 构造函数接收的参数 executor,都是同步执行的。
V8 版本 8.4,Promise 构造函数源码如下 :
PromiseConstructor(
js-implicit context: NativeContext, receiver: JSAny,
newTarget: JSAny)(executor: JSAny): JSAny {
const promiseFun = UnsafeCast<JSFunction>(
context[NativeContextSlot::PROMISE_FUNCTION_INDEX]);
let result: JSPromise;
result = NewJSPromise();
const funcs = CreatePromiseResolvingFunctions(result, True, context);
const resolve = funcs.resolve; // executor 第一个参数:resolve
const reject = funcs.reject; // executor 第二个参数:reject
try {
// 在这一行同步调用了 executor
Call(context, UnsafeCast<Callable>(executor), Undefined, resolve, reject);
} catch (e) {
Call(context, reject, Undefined, e);
}
return result;
}
从源码看 resolve 的参数是 thenable 时,有加入 microtask 队列的操作,但那是 resolve 函数的逻辑,与 executor 没有关系。
本题中 resolve 接收的参数不是 thenable 对象,自然没有加入 microtask 队列的操作,一切都是同步执行。从源码看 resolve 执行一半的时候,p 就已经是 fulfilled 状态了。
泻药
没看懂你说啥……
这个例子是同步的啊
fulfilled 了
大概你是要问这个吧……
25.4.1.3.2Promise Resolve Functions#
A promise resolve function is an anonymous built-in function that has [[Promise]] and [[AlreadyResolved]] internal slots.
When a promise resolve function F is called with argument resolution, the following steps are taken:
- Assert: F has a [[Promise]] internal slot whose value is an Object.
- Let promise be the value of F's [[Promise]] internal slot.
- Let alreadyResolved be the value of F's [[AlreadyResolved]] internal slot.
- If alreadyResolved.[[Value]] is true, return undefined.
- Set alreadyResolved.[[Value]] to true.
- If SameValue(resolution, promise) is true, then
- Let selfResolutionError be a newly created TypeError object.
- Return RejectPromise(promise, selfResolutionError).
- If Type(resolution) is not Object, then
- Return FulfillPromise(promise, resolution).
- Let then be Get(resolution, "then").
- If then is an abrupt completion, then
- Return RejectPromise(promise, then.[[Value]]).
- Let thenAction be then.[[Value]].
- If IsCallable(thenAction) is false, then
- Return FulfillPromise(promise, resolution).
- Perform EnqueueJob("PromiseJobs", PromiseResolveThenableJob, « promise, resolution, thenAction »).
- Return undefined.
The length property of a promise resolve function is 1.
25.4.1.4FulfillPromise ( promise, value)#
When the FulfillPromise abstract operation is called with arguments promise and value, the following steps are taken:
- Assert: the value of promise's [[PromiseState]] internal slot is "pending".
- Let reactions be the value of promise's [[PromiseFulfillReactions]] internal slot.
- Set the value of promise's [[PromiseResult]] internal slot to value.
- Set the value of promise's [[PromiseFulfillReactions]] internal slot to undefined.
- Set the value of promise's [[PromiseRejectReactions]] internal slot to undefined.
- Set the value of promise's [[PromiseState]] internal slot to "fulfilled".
- Return TriggerPromiseReactions(reactions, value).