Javascript 访问';这';promise回调中的对象(然后)
我想用Javascript创建一个对象 其中一个方法应该执行承诺链。链中的每个方法都必须访问作为对象成员的配置变量。 问题是,Javascript 访问';这';promise回调中的对象(然后),javascript,node.js,promise,Javascript,Node.js,Promise,我想用Javascript创建一个对象 其中一个方法应该执行承诺链。链中的每个方法都必须访问作为对象成员的配置变量。 问题是,this运算符在promisethod2中更改,我无法访问配置变量(它在promisethod1中正常工作) 这是我的密码: var SomeObject(config) { var that = this; that.config = config; } SomeObject.prototype.SomeMethod = function() {
this
运算符在promisethod2
中更改,我无法访问配置变量(它在promisethod1
中正常工作)
这是我的密码:
var SomeObject(config) {
var that = this;
that.config = config;
}
SomeObject.prototype.SomeMethod = function() {
var that = this;
that.PromiseMethod1()
.then(that.PromiseMethod2)
.catch(console.error);
}
SomeObject.prototype.PromiseMethod1 = function() {
var that = this;
config = that.config;
return SomePromise();
}
SomeObject.prototype.PromiseMethod2 = function(someParams) {
var that = this;
config = that.config;
params = someParams;
return SomePromise();
}
var someObject = new SomeObject(someConfig);
someObject.SomeMethod().then(function () {
console.log('Done!');
}
我想在链中使用方法委托,而不仅仅是执行:
that.PromiseMethod1().then(function(response) { return that.PromiseMethod2(that, response); };
我不能使用bind
方法,因为它看起来像是在执行回调时被重新绑定的
有解决办法吗?
为什么
承诺方法1
和承诺方法2
之间有区别?我认为这是不可能的。您尝试混合两种不同的方法:和。我建议您检查一下您的体系结构
如果你能完全控制你想要使用的所有承诺,那么唯一可见的事情(但我个人不喜欢)就是通过整个承诺链传递配置值
SomeObject.prototype.init = function() {
var that = this;
return new Promise(function(resolve, reject) {
resolve(that.config)
});
}
SomeObject.prototype.PromiseMethod1 = function(config, params) {
return SomePromise(config, params);
}
SomeObject.prototype.PromiseMethod2 = function(config, someParams) {
return SomePromise(config, someParams);
}
SomePromise = function(config, params) {
return new Promise(function(resolve, reject) {
//some code here
resolve(config, newParamsFromSomeCode)
});
}
然后您可以拨打:
that.init().then(that.PromiseMethod1).then(that.PromiseMethod2);
但同样,它看起来不是一个好代码…我真正的建议是不要使用这个或新的(如果您仍然想要继承,您可以使用对象。创建):
在这里,我使用闭包,以及它们封闭其父词法作用域变量的能力,这对我是有利的。而不是依赖JavaScript在运行时根据调用函数的对象将这个神奇地注入到我的函数中,因为正如您的问题所证明的,这并不总是有效的
但是,我知道这是一种非常规的工作方式,因此如果您想坚持使用this
,您需要使用绑定
,以便告诉JavaScript函数属于哪个神奇的this
-值:
var SomeObject function(config) {
this.config = config;
}
SomeObject.prototype.PromiseMethod1 = function(){
return somePromise(this.config.foo);
}
SomeObject.prototype.PromiseMethod1 = function(x){
return someOtherPromise(this.config.bar, x);
}
var instance = new SomeObject({config: true});
instance.PromiseMethod1().then(instance.PromiseMethod2.bind(instance)); //<- :(
var SomeObject函数(配置){
this.config=config;
}
SomeObject.prototype.PromiseMethod1=函数(){
返回somePromise(this.config.foo);
}
SomeObject.prototype.PromiseMethod1=函数(x){
返回其他承诺(this.config.bar,x);
}
var instance=newsomeobject({config:true});
然后(instance.promisethod2.bind(instance))//如何解决某个承诺?每种方法可能做不同的事情。大多数情况下,我使用“新承诺(解决,拒绝)”和“解决”(somePrams)。或者我使用第三方承诺,如ElasticSearch或RequestPromise。您声明您不能使用bind,但您尝试过吗?看来这是正确的解决方案。嗨。是的,我试过了。不起作用。请准确显示您尝试的内容。没有方法链接。所有的方法都在SomeObject下。我尝试了这个方法,问题是Promise reolve方法不支持超过1个参数。是的,但是您希望逐个调用同一对象的方法。如果您真的需要异步代码,我建议您将异步操作放在单独的对象/层/组件/任何东西中,并使用简单的链接。
var SomeObject function(config) {
this.config = config;
}
SomeObject.prototype.PromiseMethod1 = function(){
return somePromise(this.config.foo);
}
SomeObject.prototype.PromiseMethod1 = function(x){
return someOtherPromise(this.config.bar, x);
}
var instance = new SomeObject({config: true});
instance.PromiseMethod1().then(instance.PromiseMethod2.bind(instance)); //<- :(