【问题标题】:bluebird Promise chain this referencebluebird Promise 链这个参考
【发布时间】:2018-01-09 17:03:45
【问题描述】:

我正在为一个具有多个功能的类而苦苦挣扎。他们每个人都返回一个承诺。

我的问题是,在第一次返回 Promise 之后,next 函数中的 this-reference 不是类对象而是全局节点对象。

这里是一些示例代码:

index.js:

"use strict";

const debug = require("./dbg.js");
const testClass = require("./testClass.js");


const dbg = new debug();

const tc = new testClass(dbg);


tc.printTestMessage()
    .then(tc.Test1)
    .then(tc.Test2)
    .then(tc.Test3);

testClass.js:

"use strict";


const Promise = require("bluebird");

let testClass = function(dbg) {

let self = this;

self._dbg = dbg;



};

testClass.prototype.printTestMessage = function() {

    let self = this;

    return new Promise(function(resolve) {

        self._dbg.printDebug(3, "testClass - printTestMessage", "start printing...");

        setTimeout(function() {

            self._dbg.printDebug(3, "testClass - printTestMessage", "finished printing...");
        resolve();

        }, 2000);

    });

};


testClass.prototype.Test1 = function() {

    let self = this;

    return new Promise(function(resolve) {

        self._dbg.printDebug(3, "testClass - Test1", "start printing...");

        setTimeout(function() {

            self._dbg.printDebug(3, "testClass - Test1", "finished printing...");
            resolve();

        }, 2000);

    });

};


testClass.prototype.Test2 = function() {

    let self = this;

    return new Promise(function(resolve) {

        self._dbg.printDebug(3, "testClass - Test2", "start printing...");

       setTimeout(function() {

            self._dbg.printDebug(3, "testClass - Test2", "finished printing...");
            resolve();

        }, 2000);

    });

};


testClass.prototype.Test3 = function() {

    let self = this;

    return new Promise(function(resolve) {

        self._dbg.printDebug(3, "testClass - Test3", "start printing...");

        setTimeout(function() {

            self._dbg.printDebug(3, "testClass - Test3", "finished printing...");
            resolve();

        }, 2000);

    });

};




module.exports = testClass;

关于如何将每个函数粘贴到类引用中的任何帮助?

【问题讨论】:

    标签: javascript node.js promise bluebird


    【解决方案1】:

    我自己找到的:每个新的 Promise 都必须在末尾添加 .bind(self)。

    例如:

    testClass.prototype.Test3 = function() {
    
        let self = this;
    
        return new Promise(function(resolve) {
    
            self._dbg.printDebug(3, "testClass - Test3", "start printing...");
    
            setTimeout(function() {
    
                self._dbg.printDebug(3, "testClass - Test3", "finished printing...");
                resolve();
    
            }, 2000);
    
        }).bind(self); //<-- this is the important part
    
    };
    

    【讨论】:

    • afaik,Promises 没有 bind 方法,并且在使用错误的 this 对象调用的函数中使用 .bind(this) 不会解决您的错误。你必须在这里使用bind .then(tc.Test3.bind(tc))
    • @Thomas - 问题标记为“Bluebird”,Bluebird promises 确实有 .bind() 方法。
    • @Thomas 你错了,bluebird 有一个 bind 方法。那么为什么要投反对票呢?
    • 我没有投票。我有意见,jfriend纠正了我;我很好
    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2020-06-16
    • 1970-01-01
    相关资源
    最近更新 更多