Javascript 将对象绑定到 Promise.then() 参数的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31013674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The correct way to bind object to Promise.then() argument
提问by ThomasReggi
I found out the hard way that one can't simply just pass in a object's function into the Bluebird then. I'm assuming that Bluebird's thenis doing some magic and wrapping the passed in function in an anonymous function. So I attached a .bindto the function and it worked. Is this the correct way to do this with bluebird? Or is there some better way?
我发现不能简单地将对象的函数传入 Bluebird 的艰难方法then。我假设 Bluebirdthen正在做一些魔术并将传入的函数包装在匿名函数中。所以我将 a 附加.bind到该函数并且它起作用了。这是对蓝鸟执行此操作的正确方法吗?或者有什么更好的方法?
var Promise = require("bluebird")
var Chair = function(){
this.color = "red"
return this
}
Chair.prototype.build = function(wood){
return this.color + " " + wood
}
var chair = new Chair()
//var x = chair.build("cherry")
Promise.resolve("cherry")
.then(chair.build.bind(chair)) // color is undefined without bind
.then(console.log)
I know none of this is async so please bare with the sync example, my usage is async.
我知道这些都不是异步的,所以请使用同步示例,我的用法是异步的。
采纳答案by thefourtheye
So I attached a
.bindto the function and it worked. Is this the correct way to do this with bluebird?
所以我将 a 附加
.bind到该函数并且它起作用了。这是对蓝鸟执行此操作的正确方法吗?
Yes, that is one way to retain the context. You can also pass an anonymous function (you might already know this).
是的,这是保留上下文的一种方式。您还可以传递匿名函数(您可能已经知道这一点)。
Promise.resolve("cherry")
.then(function (value) {
return chair.build(value);
})
.then(console.log);
Or is there some better way?
或者有什么更好的方法?
You can actually use bluebird's Promise.bindmethod, like this
你实际上可以使用bluebird的Promise.bind方法,就像这样
Promise.resolve("cherry")
.bind(chair)
.then(chair.build)
.then(console.log)
Now, whenever the Promise handlers (fulfill handlers or rejection handlers) are called, inside the function, thiswill refer the chairobject only.
现在,无论何时调用 Promise 处理程序(履行处理程序或拒绝处理程序),在函数内部,this都将chair仅引用对象。
Note 1:In this specific case, console.logalso gets thisas chairobject, but it still works fine, because, in Node.js, console.logfunction is defined not only on the prototype bu also on the object itself, bound to the consoleobject. The corresponding code is here.
注意 1:在这种特定情况下,console.log也this作为chair对象获取,但它仍然可以正常工作,因为在 Node.js 中,console.log函数不仅定义在原型上,还定义在对象本身上,绑定到console对象。相应的代码在这里。
Note 2:If different handlers need different contexts, then better write anonymous functions. In that case, Promise.bindwill not help much. But if you choose to use it, then you have to use different contexts for each of the handlers and your code might look something like this
注意2:如果不同的处理程序需要不同的上下文,那么最好编写匿名函数。在那种情况下,Promise.bind不会有多大帮助。但是如果你选择使用它,那么你必须为每个处理程序使用不同的上下文,你的代码可能看起来像这样
var chair1 = new Chair("red")
var chair2 = new Chair("green")
Promise.resolve("cherry")
.bind(chair1) // Changing the binding to `chair1`
.then(chair1.build)
.tap(console.log)
.bind(chair2) // Changing the binding to `chair2`
.then(chair2.build)
.tap(console.log);

