访问promise回调中的对象'this'(然后)

shu*_*ima 6 javascript node.js promise

我想在Javascript中创建一个对象.

其中一个方法应该执行一个promise链.链中的每个方法都必须访问作为对象成员的配置变量.问题是,this操作员已更改PromiseMethod2,我无法访问配置变量(它正常工作PromiseMethod1).

这是我的代码:

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!');
}
Run Code Online (Sandbox Code Playgroud)

我想在链中使用方法委托而不是仅执行:

 that.PromiseMethod1().then(function(response) { return that.PromiseMethod2(that, response); };
Run Code Online (Sandbox Code Playgroud)

我无法使用该bind方法,因为它在执行回调时看起来像被重新绑定.

这个问题有方法解决吗?为什么PromiseMethod1和之间存在差异PromiseMethod2

Ava*_*vaq 1

真正的建议是不要使用thisnew根本不使用(Object.create如果您仍然想要继承,则可以使用):

var SomeObject = function(config) {
    return {
        PromiseMethod1: function(){
            return somePromise(config.foo);
        },
        PromiseMethod2: function(x){
            return someOtherPromise(config.bar, x);
        }
    }
}

var instance = SomeObject({config: true});
instance.PromiseMethod1().then(instance.PromiseMethod2);
Run Code Online (Sandbox Code Playgroud)

在这里,我使用闭包,以及它们封装其父词法范围的变量的能力,这对我来说是有利的。而不是依赖 JavaScriptthis在运行时根据调用函数的对象神奇地将 a 注入到我的函数中,因为正如您的问题所证明的那样,这并不总是有效。

但是,我知道这是一种非常规的工作方式,因此如果您愿意坚持使用this,则需要使用bind来告诉 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)); //<- :(
Run Code Online (Sandbox Code Playgroud)

在您的示例中,SomeMethod您实际上并未使用bind. 您仍然需要绑定,因为您将函数传递到.then(f),并且接收函数的代码不再知道它应该用于 的哪个对象this。现在再看一下我之前推荐的代码。那里没有thisses,所以这些函数根本不依赖于它们被调用的对象,您可以根据需要将它们作为高阶函数传递,而无需bindthat = this。:)