拥有这些嵌套函数:
Promise.all( mapDomains( x => x.close() ) )
我考虑过编写它们:
compose(Promise.all, mapDomains)( x => x.close() )
但是上面的代码在没有将Promise.all绑定到自身的情况下失败了。这是修复:
let f1 = Promise.all.bind(Promise)
compose(f1, mapDomains)( x => x.close() )
虽然我理解这是关于this
关键字如何在Javascript中运行的所有内容,但我想知道:为什么.all
已经不受限制了?这有什么价值吗?
答案 0 :(得分:0)
我的猜测是这样的问题:
var Promise = {
all: function() {
console.log(this.text)
},
text: 'some text'
};
Promise.all(); //some text
function compose(f) {
f(); //Here 'this' will not be what you expect, unless you've bound it
}
compose(Promise.all); //undefined
compose(Promise.all.bind(Promise)); //some text
答案 1 :(得分:0)
您可能希望将其与另一个Promise实现一起使用
// just a demo
// this should be another library to make sense
class MyPromise extends Promise {
then(onFulfilled, onRejected) {
const wrappedOnFulfilled = function(arg) {
console.log(`I was fulfilled with ${arg}`)
return onFulfilled(arg)
}
return super.then.call(this, wrappedOnFulfilled, onRejected)
}
}
var all = Promise.all
const myPromise = all
.call(MyPromise, [MyPromise.resolve(1), MyPromise.resolve(2)])
.then(([x,y]) => console.log(x, y))
console.log(myPromise instanceof MyPromise)