是否 Promise.resolve(1);涉及微任务队列?

问题描述 投票:0回答:1

我正在搜索

Promise.resolve(1);
是否包含任何mcirotask队列?没有附加 .then() 或 .catch() 处理程序。仅 Promise.resolve(1);示例:

const v = Promise.resolve(1);

根据 ECMA 2024 规范,我已阅读以下算法:

27.2.4.7 Promise.resolve ( x )

此函数返回使用传递的参数解析的新 Promise,或者如果参数是此构造函数生成的 Promise,则返回参数本身。

1. Let C be the this value.
2. If C is not an Object, throw a TypeError exception.
3. Return ? PromiseResolve(C, x).

注意 该函数期望其 this 值是一个支持 Promise 构造函数参数约定的构造函数。

27.2.4.7.1 PromiseResolve (C, x)

抽象操作 PromiseResolve 接受参数 C(构造函数)和 x(ECMAScript 语言值),并返回包含 ECMAScript 语言值的正常完成或抛出完成。它返回一个用 x 解决的新承诺。调用时它会执行以下步骤:

1. If IsPromise(x) is true, then
a. Let xConstructor be ? Get(x, "constructor").
b. If SameValue(xConstructor, C) is true, return x.
2. Let promiseCapability be ? NewPromiseCapability(C).
3. Perform ? Call(promiseCapability.[[Resolve]], undefined, « x »).
4. Return promiseCapability.[[Promise]].

27.2.1.3.2 Promise 解析函数

promise 解析函数是一个匿名内置函数,具有 [[Promise]] 和 [[AlreadyResolved]] 内部槽。

当通过参数解析调用 Promise 解析函数时,将执行以下步骤:

1. Let F be the active function object.
2. Assert: F has a [[Promise]] internal slot whose value is an Object.
3. Let promise be F.[[Promise]].
4. Let alreadyResolved be F.[[AlreadyResolved]].
5. If alreadyResolved.[[Value]] is true, return undefined.
6. Set alreadyResolved.[[Value]] to true.
7. If SameValue(resolution, promise) is true, then
a. Let selfResolutionError be a newly created TypeError object.
b. Perform RejectPromise(promise, selfResolutionError).
c. Return undefined.
8. If resolution is not an Object, then
a. Perform FulfillPromise(promise, resolution).
b. Return undefined.
9. Let then be Completion(Get(resolution, "then")).
10. If then is an abrupt completion, then
a. Perform RejectPromise(promise, then.[[Value]]).
b. Return undefined.
11. Let thenAction be then.[[Value]].
12. If IsCallable(thenAction) is false, then
a. Perform FulfillPromise(promise, resolution).
b. Return undefined.
13. Let thenJobCallback be HostMakeJobCallback(thenAction).
14. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).
15. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
16. Return undefined.
The "length" property of a promise resolve function is 1𝔽.

参见步骤 8.a

27.2.1.4 FulfillPromise(承诺,价值)

抽象操作 FulfillPromise 接受参数 Promise(Promise)和 value(ECMAScript 语言值)并返回未使用的值。调用时它会执行以下步骤:

1. Assert: The value of promise.[[PromiseState]] is pending.
2. Let reactions be promise.[[PromiseFulfillReactions]].
3. Set promise.[[PromiseResult]] to value.
4. Set promise.[[PromiseFulfillReactions]] to undefined.
5. Set promise.[[PromiseRejectReactions]] to undefined.
6. Set promise.[[PromiseState]] to fulfilled.
7. Perform TriggerPromiseReactions(reactions, value).
8. Return unused.

我说 Promise.resolve(1) 不包含任何微任务对吗?

javascript promise es6-promise ecma
1个回答
0
投票

不,

Promise.resolve(1)
不涉及任何微任务。当你得出正确的结论时,它只会返回一个已履行的承诺。

© www.soinside.com 2019 - 2024. All rights reserved.