簡體   English   中英

訪問promise回調中的對象'this'(然后)

[英]Accessing 'this' of an object inside promise callback (then)

我想在Javascript中創建一個對象。

其中一個方法應該執行一個promise鏈。 鏈中的每個方法都必須訪問作為對象成員的配置變量。 問題是,在PromiseMethod2更改了this運算符,我無法訪問配置變量(它在PromiseMethod1正常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!');
}

我想在鏈中使用方法委托而不是僅執行:

 that.PromiseMethod1().then(function(response) { return that.PromiseMethod2(that, response); };

我不能使用bind方法,因為它看起來像在執行回調時被重新bind

這個問題有方法解決嗎? 為什么PromiseMethod1PromiseMethod2之間存在差異?

真正的建議是不要使用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);

在這里,我正在使用閉包,以及它們包含其父詞法范圍變量的能力,這對我有利。 而不是依賴於JavaScript奇跡般地注入this基於哪個對象被調用的函數上,因為你的問題,因為表現出對進入我在運行時功能,這並不總是工作。

但是,我知道它是一種非傳統的工作方式,所以如果你更願意堅持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)); //<- :(

在你的示例SomeMethod你實際上並沒有使用bind 您仍然需要綁定,因為你傳遞函數成.then(f)以及接收功能的代碼已經不知道哪個對象應使用在this 現在再看看我之前推薦的代碼。 那里沒有this ses,所以這些函數根本不依賴於它們被調用的對象,你可以將它們作為高階函數傳遞給你想要的,而不必bindthat = this :)

我會說這是不可能的。 您嘗試混合使用兩種不同的方法: 方法鏈接承諾鏈接 我建議您檢查一下您的架構。

唯一可見的東西(但我個人並不喜歡),如果你完全控制你想要使用的所有承諾,就是通過整個promises鏈傳遞配置值。

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);

但同樣,它看起來不是一個好的代碼......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM