确定未决和未解决的承诺

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

https://tc39.es/ecma262/#sec-promise-objects

如果承诺已解决或已“锁定”以匹配另一个承诺的状态,则该承诺已解决。尝试解决或拒绝已解决的承诺无效。如果未解决,则承诺未解决。 未解决的承诺总是处于未决状态。已解决的承诺可能处于待定、已履行或已拒绝状态。

根据我的理解,promise 2 unresolved 有一段时间是因为 for 正在阻塞线程,因此,它也是 pending(代码 1):

let promise1 = Promise.resolve('Hi StackOverflow');
promise1
  .then(function (value) {
    console.log('then() of promise 1: ' + value);
    for (let index = 0; index <= 10000000000; index++) {}
    console.info('finished executing the for - blocking code');
    return 2023;
  }) // promise 2 - is it UNRESOLVED and pending for some time (while the for is finishing executing)?
  .then(function (data) {
    console.log('then() of promise 2: ' + data);
    return 'a string';
  })// promise 3
  .catch((error) => {
    console.log('catch(): ' + error);
    return 1000;
  }); // promise 4

还要检查这段代码的注释,看看我是否理解正确(代码 2):

const promise1 = new Promise((resolve, reject) => {
  // unresolved and PENDING
  // more code...
  resolve('My string');
  // FROM HERE on it is already resolved
})
.then(function (value) {
  console.log('then() of promise 1: ' + value);
  return 2023;
})
.catch((error) => {
  console.log('catch(): ' + error);
  return 1000;
});

我的问题只是检查我是否知道如何识别何时承诺未解决我不想做具体的事情,只是为了理解文档中对我来说很难的那部分。

javascript asynchronous promise
© www.soinside.com 2019 - 2024. All rights reserved.