Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

javascript - 如何同步确定JavaScript Promise的状态?(How can I synchronously determine a JavaScript Promise's state?)

I have a pure JavaScript Promise (built-in implementation or poly-fill):

(我有一个纯JavaScript Promise(内置实现或poly-fill):)

var promise = new Promise(function (resolve, reject) { /* ... */ });

From the specification , a Promise can be one of:

(根据规范 ,Promise可以是以下之一:)

  • 'settled' and 'resolved'

    (“解决”和“解决”)

  • 'settled' and 'rejected'

    (“解决”和“拒绝”)

  • 'pending'

    (“待定”)

I have a use case where I wish to interrogate the Promise synchronously and determine:

(我有一个用例,希望同步查询Promise并确定:)

  • is the Promise settled?

    (承诺解决了吗?)

  • if so, is the Promise resolved?

    (如果是这样,承诺是否得到解决?)

I know that I can use #then() to schedule work to be performed asynchronously after the Promise changes state.

(我知道可以在Promise更改状态后使用#then()安排要异步执行的工作。)

I am NOT asking how to do this.

(我不是问怎么做。)

This question is specifically about synchronous interrogation of a Promise's state .

(这个问题专门关于无条件状态的同步询问 。)

How can I achieve this?

(我该如何实现?)

  ask by jokeyrhyme translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No such synchronous inspection API exists for native JavaScript promises.

(不存在用于本机JavaScript承诺的此类同步检查API。)

It is impossible to do this with native promises.

(凭本地承诺是不可能做到这一点的。)

The specification does not specify such a method.

(规范未指定这种方法。)

Userland libraries can do this, and if you're targeting a specific engine (like v8) and have access to platform code (that is, you can write code in core ) then you can use specific tools (like private symbols) to achieve this.

(Userland库可以做到这一点,如果您要针对特定??的引擎(例如v8)并有权访问平台代码 (即可以在core中编写代码),则可以使用特定的工具(例如私有符号)来实现这一目标。 。)

That's super specific though and not in userland.

(这是非常具体的,但不是在用户领域。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...