以下为ChatGPT自动生成的解答,正确与否请自行判断。

BFE.devAI解答 - JavaScript 小测验
4. Promise then callbacks II

输出如下:

1
2
undefined
3
4
undefined
10

解析:

  1. Promise.resolve(1) 返回一个 Promise 对象,状态为已解决,它的值为 1。然后,执行 .then() 方法,并把回调函数传递给 then。回调函数输出 1,然后返回值 val+1(2)。这个新的 Promise 进入已解决状态,返回值为 2。

  2. 接下来,这个新的 Promise 又执行 .then() 方法,并把第二个回调函数传递给 then。回调函数输出 2。

  3. 再次执行 .then() 方法,这次返回了未定义 (undefined),因为回调函数没有返回值。

  4. 在 .then() 方法的回调函数中,Promise.resolve(3) 返回一个新的 Promise 对象,状态为已解决,它的值为 3。然后,执行 .then() 方法,并把回调函数传递给 then。回调函数输出 3。

  5. 接下来,执行 .then() 方法,并把第二个回调函数传递给 then。这个 Promise 对象也没有返回值,因此新 Promise 对象的值为未定义 (undefined)。

  6. Promise.reject(4) 返回一个 Promise 对象,状态为拒绝,它的值为 4。然后,执行 .catch() 方法,并把回调函数传递给 catch。回调函数输出 4。

  7. 执行 .finally() 方法,并把回调函数传递给 finally。回调函数输出未定义 (undefined)。它返回值 10。

  8. 最后,这个新的 Promise 对象被传递给另一个 .then() 方法,其中传递的回调函数输出 10.