1
2
3
4
5
6
7
function bad() {
return new Promise(function(resolve) {
getOtherPromise().then(function(result) {
resolve(result.property.example);
});
})
}
If the other promise is rejected, this will happen unnoticed instead of being propagated to the new promise (where it would get handled) - and the new promise stays forever pending, which can induce leaks.
1
2
3
4
5
function good() {
return getOtherPromise().then(function(result) {
return result.property.example;
})
}
Reference
What is the explicit promise construction antipattern and how do I avoid it?
https://zhuanlan.zhihu.com/p/685103077
谈谈使用promise时候的一些反模式
ES6 Promise:模式与反模式
Promise 反模式的坑
promises 很酷,但很多人并没有理解就在用了