Promise.resolve().then(() => { throw new Error('abc') }); // Return new Promise(resolve => resolve(Promise.reject(new Error('abc')))) 为啥这两个结果等同?第二个 promise 是如何进行执行的?
1
Cat7373 341 天前
我帮你整理下格式:
Promise.resolve().then(() => { throw new Error('abc') }); new Promise(resolve => resolve(Promise.reject(new Error('abc'))) ); |
2
MinonHeart 334 天前
https://github.com/domenic/promises-unwrapping/blob/master/docs/states-and-fates.md
Fulfilled, if it has been resolved to a non-promise value, or resolved to a thenable which will call any passed fulfillment handler back as soon as possible, or resolved to another promise that is fulfilled. Rejected 也是一样的逻辑。 执行 Promise.reject(new Error('abc')) 得到 Promise<Error> 执行 resolve ,跳到外层的 Promise 返回 Promise<Promise<Error>> unwrapping (用户侧不可见) 得到 Promise<Error> |
3
supuwoerc 328 天前
then 本身就会返回一个 promise ,不写 return 的话就相当于返回了 undefined ,发生错误或者 resolve/return 了 reject 都会使得 promise 进入 rejected 状态,有 catch 的话也会进去。
|