简体   繁体   English

为什么最终在Promise之前被执行?

[英]Why is finally executed before Promise's then?

When I execute the following code, the block of code A gets executed before the block of code B. 当我执行以下代码时,代码A的代码块先于代码B的代码块执行。

return new Promise((resolve, reject) => {
  try {
    resolve()
  } finally {
    // block of code A
  }
}).then(() => {
  // block of code B
})

But I don't understand why A is executed first. 但是我不明白为什么要先执行A。

The resolution (or rejection) of a promise triggers the then corresponding to it, so I would expect block of code B to be run before block of code A. 一个承诺的分辨率(或拒绝)触发then相应于它,所以我期望码B的块到码A的块之前运行

From the doc : 文档

When either of these options (resolve/reject) happens, the associated handlers queued up by a promise's then method are called. 当这些选项(解决/拒绝)中的任何一个发生时,都将调用由promise的then方法排队的关联处理程序。

I also read this: 我也读过这样的话:

the executor is called before the Promise constructor even returns the created object 在Promise构造函数甚至返回创建的对象之前调用执行程序

the executor = the function passed as param to the Promise object. executor =作为参数传递给Promise对象的函数。

The latter quote makes me think that the try catch can be finished before the resolution or rejection handlers are triggered (before the executor function is returned from the Promise). 后一个引用使我认为可以在触发解决方案或拒绝处理程序之前(在从Promise返回执行程序函数之前)完成try catch It would explain that the finally is triggered before the Promise's then . 它将解释为,在Promise的then之前触发了finally

HOWEVER, I tried making a call to an external API with fetch and await ing for the response before continuing, hoping that the Promise function would widely have the time to return the executor function before the try catch is finished: 但是,我尝试使用fetch调用外部API,并在继续之前await响应,希望Promise函数在try catch完成之前能够有足够的时间返回执行程序函数:

return new Promise(async (resolve, reject) => {
  try {
    await fetch('https://swapi.co/api/people/1/')
    resolve()
  } finally {
    // block of code A
  }
}).then(() => {
  // block of code B
})

And it turned out that A was still executed before B. I expected the resolution handler to be triggered before A is executed, since resolve is called before A is executed. 结果发现A仍在B之前执行。我希望在A执行之前就触发解析处理程序,因为在A执行之前调用了resolve But I am wrong, and I don't understand why. 但是我错了,我也不明白为什么。

May someone explain me please? 有人可以解释一下吗?

The finally{} block is part of the try catch block. finally {}块是try catch块的一部分。 So, if you have more than one set of try catch block, then each set can have their own finally block, which will get executed when either the try block is finished or the corresponding catch block is finished. 因此,如果您有一组以上的try catch块,那么每个set可以有自己的finally块,它们将在try块完成或相应的catch块完成时执行。

The promise constructor is always executed synchronously. promise构造函数始终同步执行。 This means any code inside it will execute right away. 这意味着其中的任何代码都将立即执行。

A then block queues a function to execute after the promise is resolved. then ,promise将一个函数排队,以在诺言被解决后执行。 The function you passed to then runs after all synchronous code. 然后,传递给您的函数将在所有同步代码then运行。

console.log(1);
Promise.resolve().then(() => console.log(3));
console.log(2); // logs 1, 2, 3

The spec calls this EnqueueJob. 规范称为此EnqueueJob。 Note that you should avoid explicit construction when creating promise returning function. 请注意,创建诺言返回函数时,应避免显式构造 An async function already returns a promise automatically. 异步函数已经自动返回一个Promise。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM