English 中文(简体)
为什么在我解决允诺时,产出处于待决状态?
原标题:why when I resolve a promise, the output is in pending state?

so consider the code below, when I resolve a promise the result is in pending state in the very first moment( I know after I expand the dropdown I will see the expected result but in here I m questioning the very first moment), while I reject a promise, it shows the expected result in very first moment. so I wonder why is this happening?

法典:

 const test = new Promise((resolve, reject) => {
 resolve(Promise.resolve(78))
 })

 console.log(test);

 //output in console:Promise {<pending>}
 const test2 = new Promise((resolve, reject) => {
 reject(Promise.resolve(78))
 })
 
 console.log(test2);

 //output:promise {<rejected>: Promise}
问题回答

<Promise实际上是一份承诺:一旦做了一些工作,承诺在稍后提供某种价值。 因此,通过使用<代码>then/>>>>>> /(<>fishs/code> 方法,获得您应有的价值。

基本上,如果你有<条码>测试

const test = new Promise((resolve, reject) => {
    resolve(Promise.resolve(78)); // or reject(Promise.resolve(78));
})

你们有两种有效选择来撰写定论守则:

// Use callbacks:
test
    .then(result => console.log("It s for sure successfully completed."))
    .catch(e => console.log("It s for sure failed at this moment."));

// Use await:
try {
    const result = await test;
    console.log("It s for sure successfully completed.")
} catch (e) {
    console.log("It s for sure failed at this moment.")
}

What you re observing is just result of some internal implementation, you should not rely on that observation. This actual result could change in other JavaScript engines or in different versions.

<<>Promise>

技术解释是,<代码>Resolve <>/code>/reject的功能是“一枪”的,因为一旦你打电话其中一人,就不理会其中任一功能。

If you resolve a promise with a promise or thenable object, Promise code internally creates a new, second pair of resolve/reject functions for the promise being resolved and adds a then clause to the resolving promise to resolve or reject the promise being resolved, according to the settled state of the resolving promise.

姓名:

const test = new Promise((resolve, reject) => {
 resolve(Promise.resolve(78))
})

<代码>Resolve(Promise.resolve(78)

Promise.resolve(78).then(resolve2,reject2)

where resolve2/reject2 are a new pair of resolve/reject functions created for the promise test.

如果执行的话,<代码>then 中的条款处理人(此处称为reject2)将被置于微波中位置的“Promise Reaction Job”。 微观任务岗位的任职情况是按顺序排列的,其中<条码>测试<>/条码>至少要等到同步的代码返回之后才能完成。

简言之,你只能凭无益的诺言。

<<>Promise Rejection>>

如果有许诺的<代码>reject功能,则明确拒绝。 因此,你可以拒绝任何 Java印价值的许诺,包括任何国家的假装。

姓名

const test2 = new Promise((resolve, reject) => {
 reject(Promise.resolve(78))
 })

驳回申请可以同步进行,而test2的否决理由是promise ObjectPromise.resolve(78),而不是第78号。 解散:

const test2 = new Promise((resolve, reject) => {
 reject(Promise.resolve(78))
 })

test2.catch(reason => 
   console.log("reason is a promise: ", reason instanceof Promise)
);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签