这是我在JS中try Promise时发现的一个有趣的现象.考虑一下这段代码:
Promise.resolve(1).then(2).then(console.log);
这将输出1作为其输出.为什么本地实现的Promise API没有抛出异常?当非函数参数被忽略时,"跳过"它有什么好处呢?我真的很想得到一个答复
这篇帖子是在MDN找到的.
这是我在JS中try Promise时发现的一个有趣的现象.考虑一下这段代码:
Promise.resolve(1).then(2).then(console.log);
这将输出1作为其输出.为什么本地实现的Promise API没有抛出异常?当非函数参数被忽略时,"跳过"它有什么好处呢?我真的很想得到一个答复
这篇帖子是在MDN找到的.
现代promise 的运作方式是由ECMAScript spec人定义的.但是promise 在被添加为语言的核心部分之前就已经存在了,所以为了向后兼容,它们被设计成与社区构建的promise 的行为相匹配.社区使用的规范称为Promises/A+,相关部分如下:
2.2
then
法A promise’s then method accepts two arguments:
promise.then(onFulfilled, onRejected)
2.2.1 Both onFulfilled and onRejected are optional arguments:
2.2.1.1 If onFulfilled is not a function, it must be ignored.
2.2.1.2 If onRejected is not a function, it must be ignored.
ECMAScript在其规范中继承了这一行为.那个说明书读起来难多了,但我认为它是PerformPromiseThen的这部分
- If IsCallable(onFulfilled) is false, then
a. Let onFulfilledJobCallback be empty.
至于他们为什么 Select 这样设计,我不知道.