我google了一些绑定的解释,但仍然非常困惑。我用以下方式使用承诺绑定。它正在工作,但我不知道为什么。希望有人能解释并帮助我解决我遇到的一个小问题。下面是示例代码:节点绑定并承诺
var temp1 = function(){
return new Promise(function(resolve, reject){
resolve("hello");
});
};
var temp2 = function(para1, para2){
console.log("1:", para1);
console.log("2:", para2)
return new Promise(function(resolve, reject){
resolve(para1+" "+para2);
});
};
temp1().then(temp2.bind(null,"world")).then(function(out){
console.log(out);
});
输出:
1:世界\ 2:你好\ 世界你好\
我使用 “绑定”,因为它可以使代码清理和更容易的错误处理,否则我必须使用以下内容:
temp1().then(function(out1){
return temp2(out1, "world").then(function(out2){
console.log(out2);
});
});
除了如何绑定工作,我有以下问题:
temp1().then(temp2.bind(null,/*the second param is a property of the output from temp1*/)).then(function(out){
console.log(out);
});
我知道我可以随时打破链或增加额外链,实现它: 例如
temp1().then(function(out1){
return temp2(out1, (out1.length).to_String()).then(function(out2){
console.log(out2);
});
});
但它会真棒不要打破链条。所以我的两个问题:1.如何绑定工作,2.如何保持链(也没有额外的链),而我仍然可以访问之前的输出的属性在下一个函数的参数然后。 谢谢!
因为根据定义的论点是'任何提供'之前,结果不是“你好”,“世界”,而是“世界“, “你好”。我仍然不明白为什么参数不是'null',但世界“,你好”。初始的null似乎在某处被吃掉。 –